这是我的计划:
我无法让用户按回车键,因此程序实际上可以触发自己。我对tkinter
很新!
以下是Comboboxs的代码:
self.measurements = StringVar()
self.Combobox_Length_Left = ttk.Combobox(self.length_frame, textvariable = self.measurements, values = ('mm', 'cm', 'inches', 'feet', 'yards', 'meter', 'km', 'miles'), width = 10)
self.Combobox_Length_Left.grid(row = 2, column = 0, padx = 5, pady = 5, sticky = E)
self.measurements1 = StringVar()
self.Combobox_Length_Right = ttk.Combobox(self.length_frame, textvariable = self.measurements1, value = ('mm', 'cm', 'inches', 'feet', 'yards', 'meter', 'km', 'miles'), width = 10)
self.Combobox_Length_Right.grid(row = 2, column = 2, padx = 5, pady = 5, sticky = E)
以下是条目框的代码:
self.Text_Length_Left = IntVar()
self.Entry_Length_Left = ttk.Entry(self.length_frame, textvariable = self.Text_Length_Left,width = 13)
self.Entry_Length_Left.grid(row = 3, column = 0, padx = 5, pady = 5)
self.Text_Length_Right = IntVar()
self.Entry_Length_Right = ttk.Entry(self.length_frame, textvariable = self.Text_Length_Right,width = 13)
self.Entry_Length_Right.grid(row = 3, column = 2, padx = 5, pady = 5)
将米转换为......的代码:
as_meter = dict(mm=0.001, cm=0.01, inches=0.0254,feet=0.3048, yards=0.9144, meter=1.0,km=1000.0, miles=1609.344)
def update_length(self, *args):
self.measurements.trace('w', self.update_length)
self.measurements1.trace('w', self.update_length)
self.Text_Length_Left.trace('w', self.update_length)
try:
v = float(self.Text_Length_Left.get())
except ValueError:
v = 0.0
self.Text_Length_Left.set(str(v))
m = v * self.as_meter[self.measurements.get()]
r = m/self.as_meter[self.measurements1.get()]
self.Text_Length_Right.set("{:.3e}".format(r))
我希望能够按Enter键,程序将发挥作用,并在其他输入框中显示计算,并与其他输入框显示相同的步骤。
感谢您的帮助!非常感谢。
答案 0 :(得分:0)
您希望bind
输入密钥(tkinter called "<Return>"
):
self.Combobox_Length_Right.bind("<Return>",self. update_length)
self.Combobox_Length_Right.bind("<Return>",self. update_length)