Swap Interface On Button Press
Solution 1:
You are mixing grid
and pack
(this is a bad idea, see e.g. https://stackoverflow.com/a/3968033/3001761); change the assignment of B
to:
B = Tkinter.Button(text = "Run", command = mainprg)
B.grid(row=4, column=1)
Also, your for
loops do the same thing 9 times for no apparent reason; you should probably re-think the logic.
Solution 2:
Here is an example that make a part of what you are trying to do. Note the use a a list of reference to label that have to be replaced. If no use of clean
method is done, then you just "overwrite" the previous screen appearance and it can lead to broken interface where part of button are visible partially recovered by other objects.
import Tkinter as Tki
#constants - using this you can eventually change the interface's language by importing a different file containning these values.
__title__ = "my first program"
__run__ = "run!"#widgets contain all the currently created widgets, this will alow to destroy and replace them.global widgets
widgets = []
#---------------------------------------------------------------------------------------------------defclean():
for widget in widgets:
widget.grid_remove()
widget.destroy()
#---------------------------------------------------------------------------------------------------defcreateMainButton(master):
clean()
#it is required to specify the master, i.e. the container of each new widget as first parameter
button = Tki.Button(root, text = __run__, command = lambda : mainProg(root))
button.grid(row=0, column=0)
#have to provide a reference to the widget to allow clean destroy of it.
widgets.append(button)
#---------------------------------------------------------------------------------------------------defmainProg(master):
clean()
#define a subclassdefcreate (content, row, column):
createLabel(master, content , 20, row , column)
#create widgets
create('3', 1, 1)
create('6', 1, 2)
create('4', 1, 3)
create('2', 2, 1)
create('7', 2, 2)
create(' ', 2, 3)
create('5', 3, 1)
create('1', 3, 2)
create('8', 3, 3)
#---------------------------------------------------------------------------------------------------defcreateLabel(master, content, borderSize, row, column):
label = Tki.Label(master, text= content, borderwidth=borderSize )
label.grid(row=row,column=column)
widgets.append(label)
if __name__ == "__main__":
root = Tki.Tk()
root.title(__title__)
createMainButton(root)
root.mainloop()
I don't know what are your knowledge about Object Programming, but consider an other way to achieve your goal : extend the Tk class in order to provide access to required data inside a particular scope without defining global variables ( that is, in general, BAD! ). I personally would code it like this :
import Tkinter as Tki
#constants - using this you can eventually change the interface's language by importing a different file containning these values.
__title__ = "my first program"
__run__ = "run!"classMyProgram(Tki.Tk):
def__init__(self):
Tki.Tk.__init__(self)
self.widgets = []
self.CONSTANT_BORDERWIDTH = 20
self.title(__title__)
self.createMainButton()
defclean(self):
for widget in self.widgets:
widget.grid_remove()
widget.destroy()
defcreateMainButton(self):
self.clean()
#it is required to specify the master, i.e. the container of each new widget as first parameter
button = Tki.Button(self, text = __run__, command = self.mainProg)
button.grid(row=0, column=0)
#have to provide a reference to the widget to allow clean destroy of it.
self.widgets.append(button)
defmainProg(self):
self.clean()
#create widgets
self.createLabel('3', 1, 1)
self.createLabel('6', 1, 2)
self.createLabel('4', 1, 3)
self.createLabel('2', 2, 1)
self.createLabel('7', 2, 2)
self.createLabel(' ', 2, 3)
self.createLabel('5', 3, 1)
self.createLabel('1', 3, 2)
self.createLabel('8', 3, 3)
defcreateLabel(self,content, row, column):
label = Tki.Label(self, text= content, borderwidth=self.CONSTANT_BORDERWIDTH )
label.grid(row=row,column=column)
self.widgets.append(label)
if __name__ == "__main__":
root = MyProgram()
root.mainloop()
Anyway, if this code is too difficult to understand, forget object programming for now.
Good luck! Arthur.
Post a Comment for "Swap Interface On Button Press"