Python t仅在主循环中运行的车窗后车窗打开
我只希望在程序开始时运行“菜单”窗口,在按下“播放!”按钮时打开“线路板”窗口。但是,当程序启动时,窗口已经存在,按下“播放”按钮只是打包了功能中的所有功能Python t仅在主循环中运行的车窗后车窗打开,python,python-2.7,tkinter,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 #set
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()
从Tkinter导入*
menuw=Tk()
boardw=Tk()
菜单标题(“菜单”)
菜单配置(bg=“象牙”)
董事会名称(“寻宝!”)
董事会配置(bg=“象牙”)
rows=8#设置网格中的行数
columns=8#设置网格中的列数
size=75#设置每个正方形的大小
color1=“white”#设置一半正方形的颜色
color2=“black”#设置另一半正方形的颜色
画布宽度=列*大小
画布高度=行数*大小
def板():
行=8
列=8
尺寸=50
color1=“白色”
color2=“黑色”
画布宽度=列*大小
画布高度=行数*大小
框架(boardw)
全球画布
canvas=canvas(boardw,borderwidth=0,highlightthickness=0,width=canvas\u width,height=canvas\u height,background=“象牙”)
canvas.pack(side=“top”,fill=“both”,expand=True,padx=2,pady=2)
canvas.bind(“,刷新)
canvas1=画布(boardw,borderwidth=0,highlightthickness=0,width=Canvas\u width,height=20,background=“象牙”)
canvas1.pack(side=“bottom”,fill=“both”,expand=True,padx=4,pady=4)
黄金=0
分数=标签(boardw,text=(“分数={0}”)。格式(金色),bg=“象牙”,font=“haettenschweiler 15”)
score.pack()
钱箱=10
tcn=标签(boardw,text=(“剩余宝箱数量={0}”)。格式(宝箱),bg=“象牙”,font=“haettenschweiler 15”)
tcn.pack()
土匪=5
bn=标签(boardw,text=(“剩余的盗贼箱子数量={0}”)。格式(盗贼),bg=“象牙”,font=“haettenschweiler 15”)
bn.pack()
playerpos=[0,0]
pos=标签(boardw,text=(“position={0}”).format(playerpos),bg=“象牙”,font=“haettenschweiler 15”)
位置包()
def刷新(事件):
xsize=int((event.width-1)/列)
ysize=int((事件高度-1)/行)
大小=最小值(xsize、ysize)
画布。删除(“方形”)
颜色=颜色2
对于范围内的行(行):
颜色=颜色1如果颜色=颜色2其他颜色2
对于范围内的列(列):
x1=(列*大小)
y1=(行*大小)
x2=x1+尺寸
y2=y1+尺寸
画布。创建_矩形(x1,y1,x2,y2,outline=“黑色”,fill=颜色,tags=“正方形”)
颜色=颜色1如果颜色=颜色2其他颜色2
帆布。标签上升(“件”)
帆布。标签较低(“方形”)
canvas.pack(side=“top”,fill=“both”,expand=True,padx=4,pady=4)
def菜单():
titlel=标签(menuw,text=“寻宝!”,font=“Haettenschweiler 50”,fg=“black”,bg=“象牙”)
titlel.pack()
playb=按钮(菜单,text=“PLAY”,font=“Haettenschweiler 15”,fg=“black”,bg=“象牙色”,command=Board)
playb.pack()
quitb=按钮(menuw,text=“QUIT”,font=“Haettenschweiler 15”,fg=“black”,bg=“象牙”,command=menuw.destroy)
quitb.pack()
菜单()
menuw.mainloop()
发生这种情况是因为您正在创建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)
没有任何作用,因为您从未将其发送到布局管理器(pack、place、grid)以显示它