【问题标题】:How to change button text in tkinter? [duplicate]如何在 tkinter 中更改按钮文本? [复制]
【发布时间】:2020-03-11 12:48:55
【问题描述】:

那是记忆游戏。 我只放了一个按钮,但我有 27 个按钮,所以当我点击它时如何随机更改文本? 代码:

from tkinter import *
import random
root = Tk()
name = root.title('Pexeso')
text = ["A","B","C","D","E"]
button1 = Button(root, text="", width = 20, height = 10)
button1.grid(row=0 , column=0)
def button_click():
    if button1["text"]=="":
        button1["text"] = random.choice(text)

root.mainloop()

【问题讨论】:

标签: python tkinter


【解决方案1】:

试试这个:

from tkinter import *
import random

root = Tk()
name = root.title('Pexeso')
text = ["A","B","C","D","E"]
def button_click():
    button1["text"] = random.choice(text)
button1 = Button(root, text="", width = 20, height = 10, command=button_click)
button1.grid(row=0 , column=0)

root.mainloop()

【讨论】:

    猜你喜欢
    • 2021-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-01
    • 1970-01-01
    • 2016-08-06
    • 1970-01-01
    相关资源
    最近更新 更多