单击按钮后,X会按预期显示,但是一旦单击另一个按钮,则x会移至该按钮,并且上一个按钮将变为空白,无法再单击。
我希望在单击新按钮后保留X图像。我不知道为什么前一个按钮的X图像消失了。也不要介意我功能中被注释掉的部分。我正在研究一种在播放器1和播放器2之间切换的方法,但是我首先需要使图像真正正确显示在按钮上。
from tkinter import *
def openGame():
root.deiconify()
menu.withdraw()
e1txt.set("X Player 1: " + e1.get())
e2txt.set("O Player 2: " + e2.get())
turnlabeltxt.set(e1.get() + "'s turn")
turn = 1
def call(button):
global turn
global photo
if turn == 1: #turn%2 == 1:
photo = PhotoImage(file="X.png").subsample(15)
button.config(image=photo)
#turn += 1
# if (turn%2) == 0:
# photo = PhotoImage(file="Circle.png")
# button.config(image=photo)
# turn += 1
#Not messing with turn switching until I can get these darned images to work correctly
root = Tk()
root.title("Tic Tac Toe")
root.geometry("500x350")
root.resizable(True, True)
root.withdraw()
photo = PhotoImage(file="").subsample(15)
square = 85
e1txt = StringVar()
e2txt = StringVar()
b1 = Button(root, image=photo, compound=CENTER, height=square, width=square, command=lambda: call(b1))
b1.grid(row=0, column=0, padx=2, pady=2)
b2 = Button(root, image=photo, compound=CENTER, height=square, width=square, command=lambda: call(b2))
b2.grid(row=0, column=1, padx=2, pady=2)
b3 = Button(root, image=photo, compound=CENTER, height=square, width=square, command=lambda: call(b3))
b3.grid(row=0, column=2, padx=2, pady=2)
b4 = Button(root, image=photo, compound=CENTER, height=square, width=square, command=lambda: call(b4))
b4.grid(row=1, column=0, padx=2, pady=2)
b5 = Button(root, image=photo, compound=CENTER, height=square, width=square, command=lambda: call(b5))
b5.grid(row=1, column=1, padx=2, pady=2)
b6 = Button(root, image=photo, compound=CENTER, height=square, width=square, command=lambda: call(b6))
b6.grid(row=1, column=2, padx=2, pady=2)
b7 = Button(root, image=photo, compound=CENTER, height=square, width=square, command=lambda: call(b7))
b7.grid(row=2, column=0, padx=2, pady=2)
b8 = Button(root, image=photo, compound=CENTER, height=square, width=square, command=lambda: call(b8))
b8.grid(row=2, column=1, padx=2, pady=2)
b9 = Button(root, image=photo, compound=CENTER, height=square, width=square, command=lambda: call(b9))
b9.grid(row=2, column=2, padx=2, pady=2)
P1 = Label(root, textvariable=e1txt)
P1.grid(row=0, column=3, sticky=W)
P2 = Label(root, textvariable=e2txt)
P2.grid(row=1, column=3, sticky=W)
menu = Toplevel()
menu.title("Player names")
menu.geometry("265x80")
menu.resizable(False, False)
p1 = Label(menu, text="X Player 1:")
p1.grid(row = 0, column = 0)
p2 = Label(menu, text="O Player 2:")
p2.grid(row = 2, column = 0)
e1 = Entry(menu)
e1.grid(row=0, column=1)
e2 = Entry(menu)
e2.grid(row=2, column=1)
button = Button(menu, text="Start Game", command=openGame)
button.grid(row=9, column=9)
turnlabeltxt = StringVar()
turnlabel = Label(root, textvariable=turnlabeltxt)
turnlabel.grid(row=3, column=3)
答案 0 :(得分:0)
如果加载许多图像,则必须将它们保留在多个变量中或一个列表中。
但是您只能加载一次iamge,然后将相同的图像分配给许多按钮
开始时
photo = PhotoImage(file="").subsample(15)
photo_x = PhotoImage(file="X.png").subsample(15)
photo_o = PhotoImage(file="O.png").subsample(15)
后来没有加载
def call(button):
global turn
global photo
if turn == 1: #turn%2 == 1:
button.config(image=photo_x)
完整代码
from tkinter import *
def openGame():
root.deiconify()
menu.withdraw()
e1txt.set("X Player 1: " + e1.get())
e2txt.set("O Player 2: " + e2.get())
turnlabeltxt.set(e1.get() + "'s turn")
turn = 1
def call(button):
global turn
if turn == 1: #turn%2 == 1:
button.config(image=photo_x)
#turn += 1
# if (turn%2) == 0:
# button.config(image=photo_o)
# turn += 1
#Not messing with turn switching until I can get these darned images to work correctly
root = Tk()
root.title("Tic Tac Toe")
root.geometry("500x350")
root.resizable(True, True)
root.withdraw()
photo = PhotoImage(file="").subsample(15)
photo_x = PhotoImage(file="X.png").subsample(15)
photo_o = PhotoImage(file="Circle.png").subsample(15)
square = 85
e1txt = StringVar()
e2txt = StringVar()
b1 = Button(root, image=photo, compound=CENTER, height=square, width=square, command=lambda: call(b1))
b1.grid(row=0, column=0, padx=2, pady=2)
b2 = Button(root, image=photo, compound=CENTER, height=square, width=square, command=lambda: call(b2))
b2.grid(row=0, column=1, padx=2, pady=2)
b3 = Button(root, image=photo, compound=CENTER, height=square, width=square, command=lambda: call(b3))
b3.grid(row=0, column=2, padx=2, pady=2)
b4 = Button(root, image=photo, compound=CENTER, height=square, width=square, command=lambda: call(b4))
b4.grid(row=1, column=0, padx=2, pady=2)
b5 = Button(root, image=photo, compound=CENTER, height=square, width=square, command=lambda: call(b5))
b5.grid(row=1, column=1, padx=2, pady=2)
b6 = Button(root, image=photo, compound=CENTER, height=square, width=square, command=lambda: call(b6))
b6.grid(row=1, column=2, padx=2, pady=2)
b7 = Button(root, image=photo, compound=CENTER, height=square, width=square, command=lambda: call(b7))
b7.grid(row=2, column=0, padx=2, pady=2)
b8 = Button(root, image=photo, compound=CENTER, height=square, width=square, command=lambda: call(b8))
b8.grid(row=2, column=1, padx=2, pady=2)
b9 = Button(root, image=photo, compound=CENTER, height=square, width=square, command=lambda: call(b9))
b9.grid(row=2, column=2, padx=2, pady=2)
P1 = Label(root, textvariable=e1txt)
P1.grid(row=0, column=3, sticky=W)
P2 = Label(root, textvariable=e2txt)
P2.grid(row=1, column=3, sticky=W)
menu = Toplevel()
menu.title("Player names")
menu.geometry("265x80")
menu.resizable(False, False)
p1 = Label(menu, text="X Player 1:")
p1.grid(row = 0, column = 0)
p2 = Label(menu, text="O Player 2:")
p2.grid(row = 2, column = 0)
e1 = Entry(menu)
e1.grid(row=0, column=1)
e2 = Entry(menu)
e2.grid(row=2, column=1)
button = Button(menu, text="Start Game", command=openGame)
button.grid(row=9, column=9)
turnlabeltxt = StringVar()
turnlabel = Label(root, textvariable=turnlabeltxt)
turnlabel.grid(row=3, column=3)
root.mainloop()
编辑:代码以及其他更改:
#from tkinter import * # not preferred
import tkinter as tk
# --- function ---
def open_game():
root.deiconify()
menu.withdraw()
turn_name_var.set( name1_var.get() )
def call(button):
global turn
if turn%2 == 1:
button.config(image=photo_x)
turn_name_var.set( name2_var.get() )
else:
button.config(image=photo_o)
turn_name_var.set( name1_var.get() )
turn += 1
# --- main ---
# --- constants --- (UPPER_CASE_NAMES)
SQUARE = 85
# --- variables ---
turn = 1
# ---
root = tk.Tk()
root.title("Tic Tac Toe")
#root.geometry("500x350")
#root.resizable(False, False)
root.withdraw()
# ---
name1_var = tk.StringVar()
name2_var = tk.StringVar()
turn_name_var = tk.StringVar()
# ---
photo = tk.PhotoImage(file="").subsample(15)
#photo_x = tk.PhotoImage(file="X.png").subsample(15)
#photo_o = tk.PhotoImage(file="O.png").subsample(15)
photo_x = tk.PhotoImage(file="square-1.png")#.subsample(15)
photo_o = tk.PhotoImage(file="square-2.png")#.subsample(15)
# ---
frame = tk.Frame(root)
frame.pack(padx=10, pady=10)
#buttons = []
for x in range(3):
for y in range(3):
b = tk.Button(frame, image=photo, compound='center', height=SQUARE, width=SQUARE)
b['command'] = lambda c=b:call(c)
b.grid(row=y*2, column=x, padx=2, pady=2, rowspan=2)
#buttons.append(b)
tk.Label(frame, text="X Player 1:").grid(row=0, column=3)
tk.Label(frame, textvar=name1_var).grid(row=1, column=3)
tk.Label(frame, text="O Player 2:").grid(row=2, column=3)
tk.Label(frame, textvar=name2_var).grid(row=3, column=3)
tk.Label(frame, text="Turn:").grid(row=4, column=3)
tk.Label(frame, textvar=turn_name_var).grid(row=5, column=3)
# ---
menu = tk.Toplevel()
menu.title("Player names")
#menu.geometry("265x80")
menu.resizable(False, False)
tk.Label(menu, text="X Player 1:").grid(row=0, column=0)
tk.Label(menu, text="O Player 2:").grid(row=1, column=0)
tk.Entry(menu, textvar=name1_var).grid(row=0, column=1)
tk.Entry(menu, textvar=name2_var).grid(row=1, column=1)
tk.Button(menu, text="Start Game", command=open_game).grid(row=2, column=0, columnspan=2)
# ---
root.mainloop()