【问题标题】:How to get a label to appear on button press in python 3's tkinter如何让标签出现在python 3的tkinter中的按钮按下
【发布时间】: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('&lt;Button-1&gt;',...) - Buttoncommand= 并且你可以通过做两件事来分配功能 - playhide_me
  • bind('&lt;Button-1&gt;',...) 捕捉鼠标点击并且不会发送到按钮,因此它不会执行分配给command= 的功能。仅使用 command= 和 ie。 play('C', 2, C2) 隐藏C2
  • @JoranBeasley:你不需要使用StringVar。这是一种解决方案,但不是唯一的解决方案。您可以直接更改文本而不使用StringVar

标签: python python-3.x button tkinter label


【解决方案1】:

问题是因为您使用了bind('&lt;Button-1&gt;', ...),它捕获点击并且按钮不执行分配给command=的功能

仅使用command= 并将小部件发送到play()

import tkinter as tk

# --- functions ---

def play(x, y, widget):

    widget.place_forget()

    l = tk.Label(root, text='HI')
    l.place(x=x,y=y)

# --- main ---

root = tk.Tk()
root.geometry('400x400')
root.configure(bg='#FFFFFF')
root.title('Noughts and Crosses')

l = tk.Label(text='Noughts and Crosses', bg='#FFFFFF')
l.place(x=160,y = 50)

for r, row in enumerate(('A', 'B', 'C'), 1):
    for c, col in enumerate(('1', '2', '3'), 1):
        name = row + col
        x = c*100
        y = r*100
        b = Button(root, text=name)
        b['command'] = lambda x0=x,y0=y,b0=b: play(x0, y0, b0)
        b.place(x=x, y=y)

root.mainloop()

我使用for 循环使其更简单,但for 中使用的lambda 需要x0=x,y0=y,b0=b 将正确的参数发送到play

lambda x0=x,y0=y,b0=b: play(x0, y0, b0)

顺便说一句:如果你使用

var = Widget(...).place(...)
var = Widget(...).grid(...)
var = Widget(...).pack(...)

然后将None 分配给var,因为place()/grid()/pack() 返回None

你必须分两步完成

var = Widget(...)
var.place(...)

var = Widget(...)
var.grid(...)

var = Widget(...)
var.pack(...)

或者如果你不需要var

Widget(...).place(...)
Widget(...).grid(...)
Widget(...).pack(...)

【讨论】:

    【解决方案2】:

    用标签替换单个按钮很容易。稍微困难的是用标签替换几个按钮中的任何一个。这需要将访问特定按钮所需的信息与通用替换功能打包在一起。

    import tkinter as tk
    root = tk.Tk()
    
    def replace_at(i,j):
        items[i][j].grid_forget()
        label = tk.Label(root, text='Label {}, {}'.format(i,j))
        label.grid(row=i, column=j)
        items[i][j] = label
    
    items = []
    for i in range(3):
        row = []
        for j in range(3):
            button = tk.Button(root, text='Button {}, {}'.format(i,j),
                               command=lambda i=i, j=j: replace_at(i,j))
            button.grid(row=i, column=j)
            row.append(button)
        items.append(row)
    
    root.mainloop()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-23
      • 2020-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多