簡體   English   中英

tkinter - 單選按鈕可以控制多個變量嗎?

[英]tkinter - Can a radio button control multiple variables?

我正在使用 tkinter 模塊編寫日歷程序,單擊網格上的每一天將打印出當天的日期(月/日/年)。 但是,我還想在底部添加另一個部分,顯示一年中的哪一天。 像這樣:

[所需輸出的屏幕截圖

例如,2020 年 1 月 1 日是一年中的第一天,3 月 20 日是一年中的第 80 天,等等。我希望我的單選按鈕能夠控制這兩個變量,日期作為字符串,和一年中的一天作為 integer。 這可能嗎?

strDate = StringVar()
labelDate = Label(frameDay, textvariable=strDate, width=28, 
                  font=("Consolas", "15", "bold")).grid(row=1, column=1)

# Creating the calendar grid
while day <= self.returnMaxDay():
    currentDay = Date(self.month, day, self.year)
    radDay = Radiobutton(frameCalendar, text=str(day), 
                         font=("Consolas", "15", "bold"), indicatoron=0, width=4, height=1, 
                         variable=strDate, value=str(currentDay.returnDayName()) + ", " 
                                                 + str(currentDay.returnMonthName()) + " "
                                                 + str(day) + ", " + str(currentDay.year))
    radDay.grid(row=row, column=weekDay)
    day += 1
    weekDay += 1

    if weekDay == 7:
        row += 1
        weekDay = 0

labelDayOfYear = Label(window, text=str(self.dayOfYear()), font=("Consolas", "20")).pack()

這是一個簡單的示例,它在Radiobutton中使用command=來運行 function ,它將文本更改為三個Labels

通常command=期望函數的名稱不帶()和 arguments 所以我使用lambda將 function 與 ZDBC161CAA5BDA82E7DABED7

command=lambda d=current_day:on_click(d))

我還使用d=current_day為每個 function 創建新變量d 如果沒有d=current_day ,所有函數都將使用對同一變量current_day的引用,並且所有函數都將使用current_day中的相同(最后一個)值。

import tkinter as tk
import datetime

# --- functions ---

def on_click(value):
    print(value)
    label_date['text'] = value.strftime('%a, %b %d, %Y')
    label_day['text'] = value.strftime('Day: %d')
    label_weekday['text'] = value.strftime('Weekday: %a')

# --- main ---

root = tk.Tk()

radiobutton_var = tk.StringVar()

year = 2020
month = 4

row = 0

for day in range(1, 32):
    try:
        current_day = datetime.datetime(year, month, day)

        date_str = current_day.strftime('%a, %m %d, %Y')
        weekday = current_day.weekday()

        radiobutton_day = tk.Radiobutton(root,
                                text=str(day), 
                                indicatoron=0,
                                variable=radiobutton_var,
                                value=date_str,
                                #value=day,
                                command=lambda x=current_day:on_click(x))

        radiobutton_day.grid(row=row, column=weekday, sticky='we')

        if weekday == 6:
            row += 1
            radiobutton_day['bg'] = '#faa'

    except ValueError as ex:
        print(ex) 
        break

label_date = tk.Label(root, text='?')
label_date.grid(row=10, columnspan=10)

label_day = tk.Label(root, text='Day: ?')
label_day.grid(row=11, columnspan=10)

label_weekday = tk.Label(root, text='Weekday: ?')
label_weekday.grid(row=12, columnspan=10)

root.mainloop()

在此處輸入圖像描述


順便說一句:我使用datetime生成日期,並在日期不正確時try/except來捕獲錯誤。

我還使用strftime來格式化日期 - 在strftime.org頁面上查看更多信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM