0
"メニュー"ウィンドウのみをプログラムの先頭で実行し、 "ボード"ウィンドウを "PLAY!"ボタンが押されていますが、プログラムが開始されたときにウィンドウがすでに表示されていて、再生ボタンを押すだけでその機能のすべての機能がパックされます。ほとんどすべての場合においてメインループで実行されているウィンドウの後ろにTkinterウィンドウが開きます
menuw = Tk()
boardw = Tk()
、あなたがToplevel()
を使用し、代わりに、新しいウィンドウを作成するために、これを行うにはしたくありません:あなたはTkinterの2つのインスタンスを作成しているので、
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()