2014-10-10 61 views
0

我是新的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() 

我想显示的第一界面和运行按钮。按下运行按钮后,将显示第二个界面。但运行上述代码后,它不显示任何内容。

回答

1

你在混合gridpack(这是一个坏主意,请参阅,例如https://stackoverflow.com/a/3968033/3001761);改变B来分配:

B = Tkinter.Button(text = "Run", command = mainprg) 
B.grid(row=4, column=1) 

此外,您for循环做同样的事情没有明显原因的9倍;你应该重新考虑逻辑。

+0

这很好。非常感谢您的帮助。 – 2014-10-10 11:47:09

+0

如果我只想按一下Run按钮就可以显示上述一系列界面,我该怎么办? – 2014-10-10 11:53:09

+0

@生物恐怖我不知道你想达到什么目的,但是你可能会发现这很有用:http://stackoverflow.com/a/26213779/3001761 – jonrsharpe 2014-10-10 12:49:07

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() 

无论如何,如果这段代码太难理解了,现在就忘记对象编程。

祝你好运! 亚瑟。