我是Tkinter的新手,我想创建一个按钮,按下时会显示第二个界面。我写了以下程序:
import Tkinter
root = Tkinter.Tk( )
root.title("My First Game")
for r in range(3):
for c in range(3):
Tkinter.Label(root, text= '3', borderwidth=20 ).grid(row=1,column=1)
Tkinter.Label(root, text= '6', borderwidth=20 ).grid(row=1,column=2)
Tkinter.Label(root, text= '4', borderwidth=20 ).grid(row=1,column=3)
Tkinter.Label(root, text= '2', borderwidth=20 ).grid(row=2,column=1)
Tkinter.Label(root, text= '7', borderwidth=20 ).grid(row=2,column=2)
Tkinter.Label(root, text= ' ', borderwidth=20 ).grid(row=2,column=3)
Tkinter.Label(root, text= '5', borderwidth=20 ).grid(row=3,column=1)
Tkinter.Label(root, text= '1', borderwidth=20 ).grid(row=3,column=2)
Tkinter.Label(root, text= '8', borderwidth=20 ).grid(row=3,column=3)
def mainprg():
for r in range(3):
for c in range(3):
Tkinter.Label(root, text= '3', borderwidth=20 ).grid(row=1,column=1)
Tkinter.Label(root, text= '6', borderwidth=20 ).grid(row=1,column=2)
Tkinter.Label(root, text= ' ', borderwidth=20 ).grid(row=1,column=3)
Tkinter.Label(root, text= '2', borderwidth=20 ).grid(row=2,column=1)
Tkinter.Label(root, text= '7', borderwidth=20 ).grid(row=2,column=2)
Tkinter.Label(root, text= '4', borderwidth=20 ).grid(row=2,column=3)
Tkinter.Label(root, text= '5', borderwidth=20 ).grid(row=3,column=1)
Tkinter.Label(root, text= '1', borderwidth=20 ).grid(row=3,column=2)
Tkinter.Label(root, text= '8', borderwidth=20 ).grid(row=3,column=3)
B = Tkinter.Button(text = "Run", command = mainprg)
B.pack()
root.mainloop()
我正在尝试显示第一个界面和运行按钮。按 Run 按钮后,将显示第二个界面。但运行上面的代码后,它不会显示任何内容。
答案 0 :(得分:1)
您正在混合grid
和pack
(这是个坏主意,请参阅https://stackoverflow.com/a/3968033/3001761);将B
的分配更改为:
B = Tkinter.Button(text = "Run", command = mainprg)
B.grid(row=4, column=1)
此外,你的for
循环做了同样的事情9次,没有明显的理由;你应该重新考虑逻辑。
答案 1 :(得分:0)
以下是一个示例,它可以帮助您完成所需的操作。请注意,使用必须替换的标签引用列表。如果没有使用clean
方法,那么您只需“覆盖”前一个屏幕外观,它可能导致界面损坏,其中部分按钮可见,其他对象部分恢复。
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 = []
#---------------------------------------------------------------------------------------------------
def clean():
for widget in widgets:
widget.grid_remove()
widget.destroy()
#---------------------------------------------------------------------------------------------------
def createMainButton(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)
#---------------------------------------------------------------------------------------------------
def mainProg(master):
clean()
#define a subclass
def create (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)
#---------------------------------------------------------------------------------------------------
def createLabel(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()
我不知道你对对象编程有什么了解,但考虑另一种实现目标的方法:扩展Tk类,以便在不定义全局变量的情况下提供对特定范围内所需数据的访问(即,一般来说,坏!)。我个人会这样编码:
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!"
class MyProgram(Tki.Tk):
def __init__(self):
Tki.Tk.__init__(self)
self.widgets = []
self.CONSTANT_BORDERWIDTH = 20
self.title(__title__)
self.createMainButton()
def clean(self):
for widget in self.widgets:
widget.grid_remove()
widget.destroy()
def createMainButton(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)
def mainProg(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)
def createLabel(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()
无论如何,如果这段代码太难理解,暂时忘记对象编程。
祝你好运! 亚瑟。