【发布时间】:2019-12-11 23:21:42
【问题描述】:
单击按钮时,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)
【问题讨论】:
-
global可能在这里引起了问题,但我没有深入研究故障排除。 -
我更新了原始帖子,因为我最初发布问题时遇到问题。您能否查看一下新帖子,看看您是否认为全球仍然是一个问题?
-
可能。我认为正在发生的事情是您正在将
photo更新到另一个位置,从而移动它而不是添加另一个位置。我想如果你只是把global photo从call函数中去掉,那将会消失。没有承诺可以解决这个问题。 -
也许你应该在开始时只加载一次,然后分配给不同的按钮,而不是多次加载同一个图像。
-
每次单击按钮时都会重新创建
photo,因此最后一个图像将被丢弃。您只需创建一次photo。
标签: python image button tkinter