只需单击“运行”按钮即可显示一系列界面

时间:2014-10-11 13:18:18

标签: python tkinter

我编写了一个包含大约40个接口的程序,这些接口将同时显示。代码如下:

import Tkinter

root = Tkinter.Tk(  )
root.title("8 Puzzle Game")

Tkinter.Label(root, text= '4', borderwidth=25 ).grid(row=1,column=1)
Tkinter.Label(root, text= ' ', borderwidth=25 ).grid(row=1,column=2)
Tkinter.Label(root, text= '3', borderwidth=25 ).grid(row=1,column=3)
Tkinter.Label(root, text= '2', borderwidth=25 ).grid(row=2,column=1)
Tkinter.Label(root, text= '8', borderwidth=25 ).grid(row=2,column=2)
Tkinter.Label(root, text= '5', borderwidth=25 ).grid(row=2,column=3)
Tkinter.Label(root, text= '7', borderwidth=25 ).grid(row=3,column=1)
Tkinter.Label(root, text= '6', borderwidth=25 ).grid(row=3,column=2)
Tkinter.Label(root, text= '1', borderwidth=25 ).grid(row=3,column=3)

def mainprg():

    Tkinter.Label(root, text= '4', borderwidth=25 ).grid(row=1,column=1)
    Tkinter.Label(root, text= '8', borderwidth=25 ).grid(row=1,column=2)
    Tkinter.Label(root, text= '3', borderwidth=25 ).grid(row=1,column=3)
    Tkinter.Label(root, text= '2', borderwidth=25 ).grid(row=2,column=1)
    Tkinter.Label(root, text= ' ', borderwidth=25 ).grid(row=2,column=2)
    Tkinter.Label(root, text= '5', borderwidth=25 ).grid(row=2,column=3)
    Tkinter.Label(root, text= '7', borderwidth=25 ).grid(row=3,column=1)
    Tkinter.Label(root, text= '6', borderwidth=25 ).grid(row=3,column=2)
    Tkinter.Label(root, text= '1', borderwidth=25 ).grid(row=3,column=3)

    B.configure(command = mainprg1)

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



def mainprg1():
    Tkinter.Label(root, text= '4', borderwidth=25 ).grid(row=1,column=1)
    Tkinter.Label(root, text= '8', borderwidth=25 ).grid(row=1,column=2)
    Tkinter.Label(root, text= '3', borderwidth=25 ).grid(row=1,column=3)
    Tkinter.Label(root, text= '2', borderwidth=25 ).grid(row=2,column=1)
    Tkinter.Label(root, text= '5', borderwidth=25 ).grid(row=2,column=2)
    Tkinter.Label(root, text= ' ', borderwidth=25 ).grid(row=2,column=3)
    Tkinter.Label(root, text= '7', borderwidth=25 ).grid(row=3,column=1)
    Tkinter.Label(root, text= '6', borderwidth=25 ).grid(row=3,column=2)
    Tkinter.Label(root, text= '1', borderwidth=25 ).grid(row=3,column=3)

    B.configure(command = mainprg2)
def mainprg2():
    Tkinter.Label(root, text= '4', borderwidth=25 ).grid(row=1,column=1)
    Tkinter.Label(root, text= '8', borderwidth=25 ).grid(row=1,column=2)
    Tkinter.Label(root, text= '3', borderwidth=25 ).grid(row=1,column=3)
    Tkinter.Label(root, text= '2', borderwidth=25 ).grid(row=2,column=1)
    Tkinter.Label(root, text= '5', borderwidth=25 ).grid(row=2,column=2)
    Tkinter.Label(root, text= '1', borderwidth=25 ).grid(row=2,column=3)
    Tkinter.Label(root, text= '7', borderwidth=25 ).grid(row=3,column=1)
    Tkinter.Label(root, text= '6', borderwidth=25 ).grid(row=3,column=2)
    Tkinter.Label(root, text= ' ', borderwidth=25 ).grid(row=3,column=3)

    B.configure(command = mainprg3)

root.mainloop()

在这里,我每次都必须按下“运行”按钮才能显示下一个界面。我该怎么做才能同时显示它们?

1 个答案:

答案 0 :(得分:0)

如果您的意思是"按一下按钮,然后连续运行它们"你可以通过替换

来做到这一点
B.configure(command = mainprgX)

通过

root.after(1000, mainprgX)

其中X必须由实际数字替换。

这将在每个函数结束时触发一秒计时器以启动下一个计时器。

所以新代码将是:

import Tkinter

root = Tkinter.Tk(  )
root.title("8 Puzzle Game")

Tkinter.Label(root, text= '4', borderwidth=25 ).grid(row=1,column=1)
Tkinter.Label(root, text= ' ', borderwidth=25 ).grid(row=1,column=2)
Tkinter.Label(root, text= '3', borderwidth=25 ).grid(row=1,column=3)
Tkinter.Label(root, text= '2', borderwidth=25 ).grid(row=2,column=1)
Tkinter.Label(root, text= '8', borderwidth=25 ).grid(row=2,column=2)
Tkinter.Label(root, text= '5', borderwidth=25 ).grid(row=2,column=3)
Tkinter.Label(root, text= '7', borderwidth=25 ).grid(row=3,column=1)
Tkinter.Label(root, text= '6', borderwidth=25 ).grid(row=3,column=2)
Tkinter.Label(root, text= '1', borderwidth=25 ).grid(row=3,column=3)

def mainprg():

    Tkinter.Label(root, text= '4', borderwidth=25 ).grid(row=1,column=1)
    Tkinter.Label(root, text= '8', borderwidth=25 ).grid(row=1,column=2)
    Tkinter.Label(root, text= '3', borderwidth=25 ).grid(row=1,column=3)
    Tkinter.Label(root, text= '2', borderwidth=25 ).grid(row=2,column=1)
    Tkinter.Label(root, text= ' ', borderwidth=25 ).grid(row=2,column=2)
    Tkinter.Label(root, text= '5', borderwidth=25 ).grid(row=2,column=3)
    Tkinter.Label(root, text= '7', borderwidth=25 ).grid(row=3,column=1)
    Tkinter.Label(root, text= '6', borderwidth=25 ).grid(row=3,column=2)
    Tkinter.Label(root, text= '1', borderwidth=25 ).grid(row=3,column=3)

    root.after(1000, mainprg1)

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



def mainprg1():
    Tkinter.Label(root, text= '4', borderwidth=25 ).grid(row=1,column=1)
    Tkinter.Label(root, text= '8', borderwidth=25 ).grid(row=1,column=2)
    Tkinter.Label(root, text= '3', borderwidth=25 ).grid(row=1,column=3)
    Tkinter.Label(root, text= '2', borderwidth=25 ).grid(row=2,column=1)
    Tkinter.Label(root, text= '5', borderwidth=25 ).grid(row=2,column=2)
    Tkinter.Label(root, text= ' ', borderwidth=25 ).grid(row=2,column=3)
    Tkinter.Label(root, text= '7', borderwidth=25 ).grid(row=3,column=1)
    Tkinter.Label(root, text= '6', borderwidth=25 ).grid(row=3,column=2)
    Tkinter.Label(root, text= '1', borderwidth=25 ).grid(row=3,column=3)

    root.after(1000, mainprg2)
def mainprg2():
    Tkinter.Label(root, text= '4', borderwidth=25 ).grid(row=1,column=1)
    Tkinter.Label(root, text= '8', borderwidth=25 ).grid(row=1,column=2)
    Tkinter.Label(root, text= '3', borderwidth=25 ).grid(row=1,column=3)
    Tkinter.Label(root, text= '2', borderwidth=25 ).grid(row=2,column=1)
    Tkinter.Label(root, text= '5', borderwidth=25 ).grid(row=2,column=2)
    Tkinter.Label(root, text= '1', borderwidth=25 ).grid(row=2,column=3)
    Tkinter.Label(root, text= '7', borderwidth=25 ).grid(row=3,column=1)
    Tkinter.Label(root, text= '6', borderwidth=25 ).grid(row=3,column=2)
    Tkinter.Label(root, text= ' ', borderwidth=25 ).grid(row=3,column=3)

    root.after(1000, mainprg3)

root.mainloop()