Tkinter window open behind only window running in mainloop(仅主回路中运行的窗口后的窗口打开)

2024-05-26 07:45:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我只希望“菜单”窗口在程序开始时运行,而“板”窗口在“播放”时打开按钮被按下,但是当程序启动时窗口已经在那里,按下播放按钮只会将所有功能打包到函数中。在

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

Tags: columnsofthetextsizewidthpackrows
1条回答
网友
1楼 · 发布于 2024-05-26 07:45:16

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

menuw = Tk()
boardw = Tk()

在几乎所有的情况下,您不想这样做,而是要创建一个新窗口,为您的板使用Toplevel(),并允许{}作为根窗口:

^{pr2}$

另外,这一行:Frame(boardw)没有做任何事情,因为您从未将它发送到布局管理器(pack,place,grid)来显示它。在

相关问题 更多 >