Python Spin Boxes互相复制,我不明白为什么?

时间:2016-09-24 00:45:48

标签: python tkinter

我正在编写一个代码来创建时间日历,由于某种原因,开始和结束时间拨号相互镜像。我已经查看了所有内容,但我看不出代码为什么会这样做的任何理由。

这是代码?

from Tkinter import *
import math
Master = Tk()

def Value_Check():
    Start_Hours = eval(Starting_Hours.get())
    Start_Min = eval(Starting_Minutes.get())
    End_Hours = eval(Ending_Hours.get())
    End_Min = eval(Ending_Minutes.get())

    Start_Time_Window = ((Start_Hours*60)+ Start_Min)
    End_Time_Window = ((End_Hours*60)+ End_Min)
    Total_Window = (Start_Time_Window - End_Time_Window)
    Window_Hours = math.floor(Total_Window/60)
    Window_Minutes = (Total_Window - Window_Hours)

    print "You have a ", Window_Hours, "Hours and", Window_Minutes, "minute window to test"


Frame_Start_Window= Frame(Master)
Frame_Start_Window.pack()

#Setting the starting time of the testing window
Start_Time_Frame = Frame(Master)
Start_Time_Frame.pack( side = BOTTOM )

Starting_Title = Label(Frame_Start_Window, text = "When can you start testing?                      ")
Starting_Title.pack()

Starting_Hours = Spinbox(Frame_Start_Window, text =  "Hour", from_ = 1, to =  24, wrap =True, width = 2, command = Value_Check)
Starting_Hours.pack(side = LEFT)

Collen_Title = Label(Frame_Start_Window, text = ":")
Collen_Title.pack(side = LEFT)

Starting_Minutes = Spinbox(Frame_Start_Window, text =  "Minutes", from_ = 0, to = 59, wrap =True, width = 2, command = Value_Check)
Starting_Minutes.pack(side = LEFT)

#The end half of the testing window:
Frame_End_Window= Frame(Master)
Frame_End_Window.pack()

#Setting the starting time of the testing window:
End_Title = Label(Frame_End_Window, text = "What time do you HAVE to stop testing?")
End_Title.pack()

Ending_Hours = Spinbox(Frame_End_Window, text =  "Hour", from_ = 1, to = 24,  wrap =True, width = 2, command = Value_Check)
Ending_Hours.pack(side = LEFT)

Collen2_Title = Label(Frame_End_Window, text = ":")
Collen2_Title.pack(side = LEFT)

Ending_Minutes = Spinbox(Frame_End_Window, text =  "Minutes", from_ = 0, to = 59, wrap =True, width = 2, command = Value_Check)
Ending_Minutes.pack(side = LEFT)

#Where the answer from the Test_Calculator button is displayed:
Results_Screen = Text(Master, height=2, width=65)
Results_Screen.pack()


Data_Reset = Button (Master, text = "Reset Values", command = Value_Check)
Data_Reset.pack()

mainloop()

1 个答案:

答案 0 :(得分:1)

答案是Spinbox没有text配置参数:它有textvariable,因为它接受text作为缩写。这意味着您有两个独立的Spinbox小部件,它们都使用textvariable Hour和两个独立的Spinbox小部件,两者都使用textvariable Minutetextvariable设置告诉Spinbox将Spinbox的内容链接到命名变量的内容;每当Spinbox发生变化时,命名变量都会发生变化,只要命名变量发生变化,Spinbox就会发生变化。因此,您在一个Spinbox中更改了值,它会更新变量,从而更新另一个Spinbox。

相关问题