【发布时间】:2016-12-28 22:52:34
【问题描述】:
我正在尝试编写一个 noughts and crosss 程序,并希望它是当按下按钮时按钮消失并且标签出现在其位置,但是,当我运行如下所示的代码时按钮消失但是标签没有出现,任何帮助将不胜感激,如果这是一个重复的问题,我深表歉意,但在搜索和回答时我找不到合适的线程。
import sys
from tkinter import *
def hide_me(event):
event.widget.place_forget()
def play(row, column):
holdx = int()
holdy = int()
if row == 'A':
holdx = 100
elif row == 'B':
holdx = 200
elif row == 'C':
holdx = 300
else:
print('FATAL ERROR')
if column == 1:
holdy = 100
elif column == 2:
holdy = 200
elif column == 3:
holdy = 300
else:
print('FATAL ERROR')
placement = Label(text='HI').place(x=holdx,y=holdy)
mGui = Tk()
mGui.geometry('400x400')
mGui.configure(bg='#FFFFFF')
mGui.title('Noughts and Crosses')
mLabel= Label(text='Noughts and Crosses', bg='#FFFFFF').place(x=160,y = 50)
A1 = Button(mGui, text='A1', command = lambda: play('A', 2))
A1.bind('<Button-1>', hide_me)
A1.place(x=100,y=100)
A2 = Button(mGui, text='A2', command = lambda: play('A', 2))
A2.bind('<Button-1>', hide_me)
A2.place(x=200,y=100)
A3 = Button(mGui, text='A3', command = lambda: play('A', 3))
A3.bind('<Button-1>', hide_me)
A3.place(x=300,y=100)
B1 = Button(mGui, text='B1', command = lambda: play('B', 1))
B1.bind('<Button-1>', hide_me)
B1.place(x=100,y=200)
B2 = Button(mGui, text='B2', command = lambda: play('B', 2))
B2.bind('<Button-1>', hide_me)
B2.place(x=200,y=200)
B3 = Button(mGui, text='B3', command = lambda: play('B', 3))
B3.bind('<Button-1>', hide_me)
B3.place(x=300,y=200)
C1 = Button(mGui, text='C1', command = lambda: play('C', 1))
C1.bind('<Button-1>', hide_me)
C1.place(x=100,y=300)
C2 = Button(mGui, text='C2', command = lambda: play('C', 2))
C2.bind('<Button-1>', hide_me)
C2.place(x=200,y=300)
C3 = Button(mGui, text='C3', command = lambda: play('C', 3))
C3.bind('<Button-1>', hide_me)
C3.place(x=300,y=300)
mGui.mainloop()
提前感谢任何回答我问题的人。
【问题讨论】:
-
你需要使用
stringVar并设置variable=myStringVariable而不是text=blah -
你不需要
bind('<Button-1>',...)-Button有command=并且你可以通过做两件事来分配功能 -play和hide_me -
bind('<Button-1>',...)捕捉鼠标点击并且不会发送到按钮,因此它不会执行分配给command=的功能。仅使用command=和 ie。play('C', 2, C2)隐藏C2 -
@JoranBeasley:你不需要使用
StringVar。这是一种解决方案,但不是唯一的解决方案。您可以直接更改文本而不使用StringVar。
标签: python python-3.x button tkinter label