Edit a running tkinter program - AlfonsMittelmeyer/python-gui-messaging GitHub Wiki

For example somebody has written some program. This could be a beginners first tkinter python tkinter program.

http://www.mediafire.com/view/c527156vp8uo46a/pack0.gif/file

try:
    import tkinter as tk
except ImportError:
    import Tkinter as tk

class Application(tk.Tk):

    def __init__(self,**kwargs):
        tk.Tk.__init__(self,**kwargs)
        self.labelFrame = tk.LabelFrame(self,text='LabelFrame')
        self.labelFrame.pack()
        self.input = tk.Button(self.labelFrame,text='Input')
        self.input.pack()
        self.output = tk.Button(self.labelFrame,text='Output')
        self.output.pack()
        self.load = tk.Button(self.labelFrame,text='Load File')
        self.load.pack()

if __name__ == '__main__':
    Application().mainloop()

But it may also be a more complex program. This doesn't matter.

Here could be the question, how to give the buttons the same width? Shall the width for each button be set, but what, if there will be later a button which is more broad.

So it could be a good idea to launch the GuiDesigner in addition to this program. First we use a tkinter extension, which the GuiDesigner needs.

Instead of importing tkinter

'''
try:
    import tkinter as tk
except ImportError:
    import Tkinter as tk
'''

import DynTkInter as tk

we import DynTkInter. This will not make any difference to this program. But DynTkInter has an extented functionality. And one extended functionality is an additional parameter for mainloop. Such an additional paramater will load a DynTkInter script and such a script is the GuiDesigner:

if __name__ == '__main__':
    Application().mainloop('guidesigner/Guidesigner.py')

This program now has to be located in the GuiDesigner directory, where also is DynTkInter.py. And then in addition to the Application there also this Toplevel window is visible:

http://www.mediafire.com/view/99va9ndvkjrgg7u/pack1.gif/file

It's the GuiDesigner and it shows, that there is an application with a widget, which name is 'labelframe'. 'labelframe' is also the default name for a LabelFrame, for which no name was given. The orange arrow means, that this is a container widget and that we may look in it, if we press this arrow. Let's do it:

http://www.mediafire.com/view/4as9zdrbncusy8x/pack2.gif/file

Now we see that we are inside this widget 'labelframe', that it's really a LabelFrame, that it has a pack layout and that we also could use a place layout instead. Further we see, that there are three widgets with the name 'button'. When we select them, we see that they are really buttons and that they have a pack layout. OK, this wasn't interesting, because the application window shows that they are buttons. The question is, which button is which?

For this purpose we press 'Config ON' in the menu and then we see which text this button has:

http://www.mediafire.com/view/w36w2fz2asjhso6/pack3.gif/file

Because we later want to export the GUI with the variable names as before we rename this buttons:

http://www.mediafire.com/view/38vazu3f9i263lp/pack4.gif/file

This looks better now:

http://www.mediafire.com/view/xeiba5ih2t00yv8/pack5.gif/file

The config isn't interesting any more with menu 'Config OFF' we close it and with 'Layout ON' we look at the layout:

http://www.mediafire.com/view/tjkngurepb0os5u/pack6.gif/file

Side 'top' and anchor 'center' seem to be clear, but what is fill? Lets press the question mark:

http://www.mediafire.com/view/osqmsryhns6hk4z/pack7.gif/file

fill 'x' seems to be a good option for trying.

And indeed this was it:

http://www.mediafire.com/view/acjlxm1ra33maor/pack8.gif/file

With File Save we save it as a Gui script, which we later may load again.

And for the export we should choose 'tkinter (names)',

http://www.mediafire.com/view/w4s5cs9fr4vezel/pack9.gif/file

because this spares us to rename the buttons the next time again.

The exported code is better structured – for each container widget a class - and when exported as 'tkinter (names)' contains the names, and also the uncommented import für DynTkInter and the mainloop with the parameter for launching the GuiDesigner:

# -*- coding: utf-8 -*-

try:
    import tkinter as tk
except ImportError:
    import Tkinter as tk

#import DynTkInter as tk # for GuiDesigner


# Application definition ============================

class Application(tk.Tk):

    def __init__(self,**kwargs):
        tk.Tk.__init__(self,**kwargs)
        # widget definitions ===================================
        self.labelframe = Labelframe(self,name='#0_labelframe')
        self.labelframe.pack()

class Labelframe(tk.LabelFrame):

    def __init__(self,master,**kwargs):
        tk.LabelFrame.__init__(self,master,**kwargs)
        self.config(text='LabelFrame')
        # widget definitions ===================================
        self.input = tk.Button(self,name='#1_input',text='Input')
        self.output = tk.Button(self,name='#2_output',text='Output')
        self.load = tk.Button(self,name='#3_load',text='Load File')
        self.input.pack(fill='x')
        self.output.pack(fill='x')
        self.load.pack(fill='x')

if __name__ == '__main__':
    #Application().mainloop('guidesigner/Guidesigner.py') # for GuiDesigner
    Application().mainloop()

The code is a little bit different than before. Pack doesn't follow immediately after the widget definition, because the widget definition is in z-order and the pack is in pack order.