Tkinter窗口仅在主循环中运行的窗口后面打开

时间:2017-02-03 11:59:42

标签: python python-2.7 tkinter

我想只在程序开始时运行“菜单”窗口,并在“播放”时打开“电路板”窗口。按下按钮但是程序启动时窗口已经存在,按下播放按钮只包含功能内的所有功能。

from Tkinter import *
menuw = Tk()
boardw = Tk()
menuw.title("menu")
menuw.configure(bg="ivory")
boardw.title("Treasure Hunt!")
boardw.configure(bg="ivory")

rows = 8 #sets the number of rows in the grid
columns = 8 # sets the number of columns in the grid
size = 75 #sets the size of each square
colour1 = "white" #sets the colour of half of the squares
colour2 = "black" #sets the colour of the other half of the squares
canvas_width = columns * size 
canvas_height = rows * size 

def Board():
    rows = 8 
    columns = 8 
    size = 50 
    colour1 = "white" 
    colour2 = "black" 
    canvas_width = columns * size 
    canvas_height = rows * size 
    Frame(boardw)
    global canvas
    canvas = Canvas(boardw, borderwidth=0, highlightthickness=0, width=canvas_width, height=canvas_height, background="ivory")
    canvas.pack(side="top", fill="both", expand = True, padx=2, pady=2)
    canvas.bind("<Configure>", refresh)
    canvas1 = Canvas(boardw, borderwidth=0, highlightthickness=0, width=canvas_width, height=20, background="ivory")
    canvas1.pack(side = "bottom", fill= "both", expand = True, padx=4, pady=4)
    gold = 0 
    score = Label(boardw, text = ("score = {0}").format(gold), bg="ivory", font = "haettenschweiler 15")
    score.pack()
    treasurechests = 10
    tcn = Label(boardw, text = ("Number of treasure chests remaining = {0}").format(treasurechests), bg="ivory", font = "haettenschweiler 15")
    tcn.pack()
    bandits = 5
    bn = Label(boardw, text = ("Number of bandits chests remaining = {0}").format(bandits), bg="ivory", font = "haettenschweiler 15")
    bn.pack()
    playerpos = [0,0]
    pos = Label(boardw, text = ("position = {0}").format(playerpos), bg="ivory", font = "haettenschweiler 15")
    pos.pack()

def refresh(event):
    xsize = int((event.width-1) / columns)
    ysize = int((event.height-1) / rows)
    size = min(xsize, ysize)
    canvas.delete("square")
    colour = colour2
    for row in range(rows):
        colour = colour1 if colour == colour2 else colour2
        for col in range(columns):
            x1 = (col * size)
            y1 = (row * size)
            x2 = x1 + size
            y2 = y1 + size
            canvas.create_rectangle(x1, y1, x2, y2, outline="black", fill=colour, tags="square")
            colour = colour1 if colour == colour2 else colour2
    canvas.tag_raise("piece")
    canvas.tag_lower("square")
    canvas.pack(side = "top", fill= "both", expand = True, padx=4,pady=4)

def menu():
    titlel = Label(menuw, text = "Treasure Hunt!", font = "Haettenschweiler 50", fg = "black", bg= "ivory")
    titlel.pack()
    playb = Button(menuw, text = "PLAY", font = "Haettenschweiler 15", fg = "black", bg= "ivory", command = Board)
    playb.pack()
    quitb = Button(menuw, text = "QUIT", font = "Haettenschweiler 15", fg = "black", bg= "ivory", command = menuw.destroy)
    quitb.pack()

menu()
menuw.mainloop()

1 个答案:

答案 0 :(得分:1)

这是因为您正在创建两个Tkinter实例:

menuw = Tk()
boardw = Tk()

在几乎所有情况下,您都不希望这样做,而是创建一个新窗口,为您的电路板使用Toplevel(),并允许menuw成为根窗口:

from Tkinter import *
menuw = Tk()
menuw.title("menu")
menuw.configure(bg="ivory")

....

def Board():
    boardw = Toplevel()
    boardw.title("Treasure Hunt!")
    boardw.configure(bg="ivory")
    ....

此行:Frame(boardw)没有做任何事情,因为您从未将其发送到布局管理器(包,地点,网格)以显示它。