【问题标题】:How do I show different groups of radio buttons at different times in python using Tkinter?如何使用 Tkinter 在 python 中的不同时间显示不同组的单选按钮?
【发布时间】:2018-04-17 09:57:58
【问题描述】:
from Tkinter import *
import time
import webbrowser
import random
import Tkinter    
"""Sets the time from the buttons into a list"""
def selectedHour():
    x = hour.get()
    if not wake_time:
        wake_time.append(x)
    else:
        wake_time[0] = x
    print wake_time
    count += 1

def selectedMinute():
    x = minute.get()
    if len(wake_time) == 1:
        wake_time.append(x)
    else:
        wake_time[1] = x
    print wake_time
    count += 1

def selectedPeriod():
    x = period.get()
    if len(wake_time) == 2:
        wake_time.append(x)
    else:
        wake_time[2] = x
    print wake_time
    count += 1

wake_time = []

"""Creates Buttons"""
hour = IntVar()
minute = IntVar()
period = StringVar()
hour.set(None)
minute.set(None)
period.set(None)

count = 1

if count == 1:
    radio12 = Radiobutton(app, text = 12, value = 12, variable = hour, command = selectedHour).pack()
elif count == 2:
    radio2 = Radiobutton(app, text = 0, value = 0, variable = minute, command = selectedMinute).pack()
else:
    radio3 = Radiobutton(app, text = 'am', value = 'am', variable = period, command = selectedPeriod).pack()

app.mainloop()

有什么方法可以让我的按钮根据计数在不同时间显示?我想要的是当点击第一组按钮时,它会显示一组不同的按钮,然后一旦点击这些按钮,就会弹出第三组。

【问题讨论】:

  • Python 没有单选按钮;你在使用某种图书馆吗?如果有,是哪个?
  • Tkinter 确实有单选按钮 link
  • 是的,抱歉,我正在使用 Tkinter。
  • 你能说得更具体点吗?您是想让用户选择一个时间 [12:00 am/pm],还是希望 Tkinter 在用户选择 hr,然后打开分钟框架,然后是 am/pm 时刷新新的框架/画布。您可以使用下拉菜单来解决这个问题。
  • 当用户选择小时、分钟和上午/下午时,我正在尝试刷新 canas。通常一小时有 12 个按钮(每小时一个),分钟有 60 个按钮,然后上午/下午有 2 个按钮,但我在这个例子中缩短了它。

标签: python python-2.7 tkinter radio-button


【解决方案1】:
#!/usr/bin/python
import Tkinter
from Tkinter import *
"""Sets the time from the buttons into a list"""
def selectedHour(count):
    x = hour.get()
    if not wake_time:
        wake_time.append(x)
    else:
        wake_time[0] = x
    print wake_time
    count += 1
    frame2.tkraise() # brings frame to front

def selectedMinute(count):
    x = minute.get()
    if len(wake_time) == 1:
        wake_time.append(x)
    else:
        wake_time[1] = x
    print wake_time
    count += 1
    frame3.tkraise() # brings frame to front

def selectedPeriod(count):
    x = period.get()
    if len(wake_time) == 2:
        wake_time.append(x)
    else:
        wake_time[2] = x
    print wake_time
    count += 1
    frame4.tkraise() # brings frame to front

wake_time = []


"""Creates Buttons"""
app = Tk()

hour = IntVar()
minute = IntVar()
period = StringVar()
hour.set(None)
minute.set(None)
period.set(None)
frame1 = Frame(app)
frame2 = Frame(app)
frame3 = Frame(app)
frame4 = Frame(app)
for frame in (frame1, frame2, frame3, frame4):
    frame.grid(row=0,column=0, sticky='news') #set frame layout

count = 1


 # initial value


HOUR = [1,2,3,4,5,6,7,8,9,10,11,12]
MINUTE = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59]
PERIOD = ['AM','PM']

group1 = LabelFrame(frame1, text="Hour", padx=5, pady=5) #Hour Label
group1.pack(padx=10, pady=10)

HRoption = OptionMenu(group1, hour, *HOUR, command = lambda x:selectedHour(count)) #dropdown menu for hours
HRoption.pack()

group2 = LabelFrame(frame2, text="Minute", padx=5, pady=5) #label for minutes
group2.pack(padx=10, pady=10)

MINOption = OptionMenu(group2, minute, *MINUTE, command = lambda x:selectedMinute(count)) #dropdown menu for minutes
MINOption.pack()

group3 = LabelFrame(frame3, text="Period", padx=5, pady=5)
group3.pack(padx=10, pady=10) #label for period

MINOption = OptionMenu(group3, period, *PERIOD, command = lambda x:selectedPeriod(count)) #dropdown menu for period
MINOption.pack()
frame1.tkraise() #start the first frame
app.mainloop()

【讨论】:

  • 对不起,你能解释一下为什么方法 selectedHour() 需要“count”变量吗?抱歉,我对此很陌生。为什么它不像我那样工作?使用 count += 1 来更改显示的按钮,具体取决于计数所在
  • 你有函数 selectedHour() 使用 count 递增 1 但没有传递给它的 count 参数。
  • 您有函数 selectedHour() 使用 count 递增 1,但没有将 count 参数传递给它。 Python 逐行运行,因此您之前的 if 语句只会运行一次,返回是不可能的,这就是函数的作用所在.您也可以使用 while(count >=1) 的 while 循环并将 if 语句放入其中,但您需要一个停止条件,以便 while 循环停止运行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-14
  • 1970-01-01
  • 2014-06-01
相关资源
最近更新 更多