下拉菜单python

时间:2014-06-27 15:51:03

标签: python tkinter

您好我已经启动了一个程序来显示我的学校时间表这是我目前的代码:

from tkinter import *

master = Tk()
master2 = Tk()
var = StringVar(master)
var2 = StringVar(master2)
var.set("Day") 
var2.set("Week")

option = OptionMenu(master, var, "Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
option2 = OptionMenu(master2, var, "A", "B")
option.pack()
option2.pack()

Week = var2.get()
Day = var.get()
def ok():
    if Day == "Monday" and Week == "A":
        print("You have: \n P.S.E with Mrs Smith \n W.B.Q with Mrs Smith \n Science with Mr Martin \n Media wirh Mr Kelleher \n English with Mrs Jenkins (of mice and men)")
        input("Press enter to close")
    if Day == "Tuesday" and Week == "A":
        print("You have: \n R.M with Mr Arnold/Mr Heywood \n English with Mrs Jenkins \n I.C.T with Mr Davies \n Maths with Mr Tucker \n I.T programing with Mr Arnold ")
        input("Press enter to close")
    if Day == "Wednesday" and Week == "A":
        print("You have: \n English with Mrs Jenkins \n Science with Mr Martin \n Re with Mr Davies \n Media with Mr Kelleher \n R.M with Mr Arnold/Mr Heywood ")
        input("Press enter to close")
    if Day == "Thursday" and Week == "A":
        print("You have: \n Games With Mr Davies \n I.C.T with Mr Davies \n I.T programing with Mr Arnold \n Science with Mr Martin \n Maths with Mr Tucker")
        input("Press enter to close")
    if Day == "Friday" and Week == "A":
        print("You have: \n Maths with Mr Tucker \n English with Mrs Jenkins \n Media with Mr Kelleher \n I.T programing with Mr Arnold \n Science with Dr Burton")
        input("Press enter to close")
    if Day == "Monday" and Week == "B":
        print("You have: \n Science with Dr Burton \n Re with Mr Davies \n Welsh with Mr Kedward \n English with Mrs Jenkins (of mice and men) \n Media with Mr Kelleher")
        input("Press enter to close")
    if Day == "Tuesday" and Week == "B":
        print("You have: \n Maths with Mr Tucker \n R.M with Mr Arnold/Mr Heywood \n I.T programing with Mr Arnold \n I.C.T with Mr Davies \n Science with Dr Burton")
        input("Press enter to close")
    if Day == "Wednesday" and Week == "B":
        print("You have: \n R.M with Mr Arnold/Mr Heywood \n Media with Mr Kelleher \n English with Mrs Jenkins \n Science with Miss Fair \n I.C.T with Mr Davies")
        input("Press enter to close")
    if Day == "Thursday" and Week == "B":
        print("You have: \n Games With Mr Davies \n I.C.T with Mr Davies \n I.T programing with Mr Arnold \n Maths with Mr Tucker \n Science with Miss fair")
        input("Press enter to close")
    if Day == "Friday" and Week == "B":
        print("You have: \n Welsh with Mr Kedward \n Science with Miss Fair \n English With Mrs Jenkins \n Maths with Mr Tucker an R.M with Mr Arnold/Mr Heywood")
        input("Press enter to close")
    else:
        print("Error invalid choice")
        input("Press enter to close")


button = Button(master, text="OK", command=ok)
button2 = Button(master2, text="OK", command=ok)
button.pack()
button2.pack()
mainloop()

我想知道如何在同一个Tkinter窗口中显示两个选项,同时它没有将值设置为正确的值,例如:Day不=星期一,而Week不= A而不是=" day周"我想知道如何解决这个问题。我是新的tkinter

1 个答案:

答案 0 :(得分:0)

这里有三件事需要解决。我按照我找到它们的顺序来做它们

Tk

的多个实例
master = Tk()
master2 = Tk() # DON'T DO THIS

永远不要同时运行两个Tk个实例。如果您需要第二个窗口,请使用tkinter.Toplevel。但是,你不需要第二个窗口,所以让我们继续。

更改master

继承的所有内容

包括var2option2button2。事实上,由于我们完全放弃master2,只需删除button2

按下按钮时进行StringVar更新

这就是为什么您的功能没有收到正确的信息。您只需将DayWeek分配给var.get()var2.get()一次,并在您创建OptionMenu后立即执行此操作在他们的默认值。当您更改OptionMenu项时,DayWeek永远不会实际更改。而是做类似的事情:

def ok():
    global var, var2
    Day = var.get()
    Week = var2.get()
    if Day == ...
    ...

后记

顺便说一句,mainloop来电应为master.mainloop()。您始终从mainloop的实例运行Tk,这是您不应拥有多个实例的部分原因!在变量命名方案中也存在很多不一致之处,我将其引用到PEP8,因为它有点超出了本答案的范围。只要知道变量名称的var之类的内容是模糊而可怕的,DayWeek应该是类,如果它们像那样大写。我还强烈建议您删除"按Enter继续"线。你不需要它们。实际上,请从用户中删除您的控制台互动:制作Toplevel并在其上写一个Label

总而言之,干得好!这是很长一段时间内进入Tkinter的最正确的开始之一。