【问题标题】:Changing the name of the label更改标签名称
【发布时间】:2020-05-05 22:22:18
【问题描述】:

(不要看整个代码,不完整的:)

我有一个重置功能,可以重置标签的名称,但此功能会删除所有标签。我应该使用什么功能(可能是 Tkinter 内置的)?

from Tkinter import *

months = ['December', 'January', 'February',
          'March', 'April', 'May',
          'June', 'July', 'August',
          'September', 'October', 'November']


def reset():
    label_result.grid_forget()


def calc():
    month = entry_month.get()
    day = entry_day.get()

    if (str(month) == months[3] and 21 <= int(day) <= 31) or \
            (str(month) == months[4] and 1 <= int(day) <= 20):
        label_result['text'] = 'Aries'
    elif (str(month) == months[4] and 21 <= int(day) <= 30) or \
            (str(month) == months[5] and 1 <= int(day) <= 20):
        label_result['text'] = 'Taurus'
    elif (str(month) == months[5] and 21 <= int(day) <= 31) or \
            (str(month) == months[6] and 1 <= int(day) <= 21):
        label_result['text'] = 'Gemini'

*是未完成条件^ *

root = Tk()
root.title('Zodiacs')
root.resizable(False, False)

label_month = Label(root, width=30, font=('Ubuntu', 15), text='Enter your birth month')
label_month.grid(row=0, column=0, sticky=E)

label_day = Label(root, width=30, font=('Ubuntu', 15), text='Enter your birth day')
label_day.grid(row=1, column=0, sticky=E)

label_zodiac = Label(root, width=30, font=('Ubuntu', 15), text='Your zodiac is ')
label_zodiac.grid(row=3, column=0)

label_result = Label(root, width=30, font=('Ubuntu', 15), text=' ')
label_result.grid(row=3, column=1)

calculate = Button(text='Calculate...', command=calc)
calculate.grid(row=2, column=1, sticky=S)

reset = Button(text='Reset', command=reset)
reset.grid(row=2, column=0)

entry_month = Entry(root)
entry_month.grid(row=0, column=1, columnspan=2)

entry_day = Entry(root)
entry_day.grid(row=1, column=1)

root.mainloop()

【问题讨论】:

    标签: python user-interface tkinter grid


    【解决方案1】:

    如果你忘记了网格,你将不得不重新网格。因此,只需将文本更改为空白即可轻松解决。

    def reset():
        label_result['text']=""
    

    def reset():
        label_result.configure(text="")
    

    【讨论】:

      猜你喜欢
      • 2018-06-10
      • 2011-03-27
      • 2019-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-12
      • 2011-07-14
      相关资源
      最近更新 更多