我在Python中有一个简单的Tkinter程序,可将英尺转换为米。它有一个Label,一个Feet Entry框,一个带凹陷边框的Meters框,然后是3个按钮:Quit,Covert和Swap。
我只是想弄清楚如何让Swap按钮做到这一点。在窗口中交换英尺和米的位置,这样您就可以输入x米并将其转换为英尺(在点击转换后)。
真的,我所需要的只是如何切换位置(数学部分很容易),但我想不出逻辑是如何工作的。这是我到目前为止所做的:
import Tkinter
win = Tkinter.Tk()
win.title('Converter')
Row1 =Tkinter.Frame(win)
blank = Tkinter.Label(Row1, text=' ', font=('Courier New', 30))
blank.pack()
Row1.pack()
label = Tkinter.Label(win, text='Convert Between Feet and Meters', font=('Courier
New',30,"bold"))
label.pack()
def convert():
st = entry1.get()
v = eval(st)
if type(v) != type('Hello'):
answer.config(text=str(v*.3048))
def swap():
#here's where I need to figure out how to swap
Row2 = Tkinter.Frame(win)
fLabel = Tkinter.Label(Row2, text='Feet', justify='right', font=('Courier New', 30))
entry1 = Tkinter.Entry(Row2, width = 12, font=('Courier New', 30))
fLabel.pack(side='left')
entry1.pack(side='right')
Row2.pack()
Row3 = Tkinter.Frame(win)
mLabel = Tkinter.Label(Row3, text='Meters', justify='right', font=('Courier New',30))
answer = Tkinter.Label(Row3, text='0', width=12, relief='sunken', font=('Courier New',
30))
mLabel.pack(side='left')
answer.pack(side='left')
Row3.pack()
Row4 = Tkinter.Frame(win)
quit = Tkinter.Button(Row4, text='Quit', command = win.destroy, font=('Courier
New',30))
convert = Tkinter.Button(Row4, text='Convert', command = convert, font=('Courier
New',30))
swap = Tkinter.Button(Row4, text='Swap', command=swap, font=('Courier New',30))
quit.pack(side='left')
convert.pack(side='left')
swap.pack(side='right')
Row4.pack()
Row5 = Tkinter.Frame(win)
blank2 = Tkinter.Label(Row5, text=' ', font=('Courier New', 30))
blank2.pack()
Row5.pack()
win.mainloop()
(第一帧和最后一帧只是空间填充)提前感谢您的帮助!
答案 0 :(得分:3)
创建一个存储转换内容的变量,并让swap
函数更改变量并更新标签。要更改标签文字,您可以label['text'] = 'new text'
或label.configure(text='new text')
。以下是对代码的修改:
import Tkinter
inputmode = 'feet' # This is the variable that stores what you are converting from
win = Tkinter.Tk()
win.title('Converter')
Row1 =Tkinter.Frame(win)
blank = Tkinter.Label(Row1, text=' ', font=('Courier New', 30))
blank.pack()
Row1.pack()
label = Tkinter.Label(win, text='Convert Between Feet and Meters', font=('Courier New',30,"bold"))
label.pack()
def convert():
st = entry1.get()
v = eval(st)
if type(v) != type('Hello'):
if inputmode == 'feet': # check which way to convert
answer.config(text=str(v*.3048))
else:
answer.config(text=str(v*3.28))
def swap():
global inputmode
if inputmode == 'meters':
inputmode = 'feet'
fLabel['text'] = 'Feet' # Changes the text of the label
mLabel['text'] = 'Metres'
else:
inputmode = 'meters'
fLabel['text'] = 'Metres'
mLabel['text'] = 'Feet'
Row2 = Tkinter.Frame(win)
fLabel = Tkinter.Label(Row2, text='Feet', justify='right', font=('Courier New', 30))
entry1 = Tkinter.Entry(Row2, width = 12, font=('Courier New', 30))
fLabel.pack(side='left')
entry1.pack(side='right')
Row2.pack()
Row3 = Tkinter.Frame(win)
mLabel = Tkinter.Label(Row3, text='Meters', justify='right', font=('Courier New',30))
answer = Tkinter.Label(Row3, text='0', width=12, relief='sunken', font=('Courier New', 30))
mLabel.pack(side='left')
answer.pack(side='left')
Row3.pack()
Row4 = Tkinter.Frame(win)
quit = Tkinter.Button(Row4, text='Quit', command = win.destroy, font=('Courier New',30))
convert = Tkinter.Button(Row4, text='Convert', command = convert, font=('Courier New',30))
swap = Tkinter.Button(Row4, text='Swap', command=swap, font=('Courier New',30))
quit.pack(side='left')
convert.pack(side='left')
swap.pack(side='right')
Row4.pack()
Row5 = Tkinter.Frame(win)
blank2 = Tkinter.Label(Row5, text=' ', font=('Courier New', 30))
blank2.pack()
Row5.pack()
win.mainloop()
答案 1 :(得分:0)
带有下标的表格比代码中的硬连线选择更具扩展性。以下使用下标t0和t1来索引标签表(英尺,米)和转换因子表(.3048,3.2808)。如果您想创建额外的转换,例如摄氏到华氏,您可以在不更改代码的情况下添加到表中。
其他一些注意事项:
以下交换函数使用table / subscript方法:
from Tkinter import Tk, Frame, Label, Entry, Button
def convert():
global t1
st = entry1.get()
v = eval(st)
if type(v) != type('Hello'):
answer.config(text=str(v*factor[t1]), anchor='w')
def swap():
global t1, t2
t1, t2 = t2, t1
Label1.config(text=lbl[t1])
Label2.config(text=lbl[t2])
answer.config(text='')
win = Tk()
win.title('Converter')
fspec = ('Courier New', 30)
label = Label(win, text='Convert Between Feet and Meters', font=fspec+('bold',))
label.pack(pady=30)
Row2 = Frame(win)
Row2.pack()
t1, t2 = 0, 1
lbl = ('Feet', 'Meters')
factor = (.3048, 1./.3048)
Label1 = Label(Row2, text=lbl[t1], justify='right', font=fspec)
entry1 = Entry(Row2, width = 12, font=fspec)
Label2 = Label(Row2, text=lbl[t2], justify='right', font=fspec)
answer = Label(Row2, width=12, relief='sunken', font=fspec)
Label1.grid(row=2, column=2)
entry1.grid(row=2, column=4)
Label2.grid(row=4, column=2)
answer.grid(row=4, column=4)
Row4 = Frame(win)
quitb = Button(Row4, text='Quit', command = win.destroy, font=fspec)
convert = Button(Row4, text='Convert', command = convert, font=fspec)
swap = Button(Row4, text='Swap', command=swap, font=fspec)
quitb.pack(side='left')
convert.pack(side='left')
swap.pack(side='right')
Row4.pack(pady=30)
win.mainloop()