在 Tkinter 中交换
我有一个简单的 Python Tkinter 程序,可以将英尺转换为米。它有一个标签、一个英尺输入框、一个带有凹陷边框的米框,以及 3 个按钮:退出、隐藏和交换。
我只是想弄清楚如何让“交换”按钮做到这一点。交换窗口中“英尺”和“米”的位置,这样您就可以输入 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()
第一帧和最后一帧只是空格填充)提前感谢您的任何帮助!
I have a simple Tkinter program in Python that converts feet to meters. It has a Label, a Feet Entry box, a Meters box with a sunken border, and then 3 buttons: Quit, Covert, and Swap.
I'm just trying to figure out how to make the Swap button do exactly that. Swap the position of Feet and Meters in the window, so you can enter x meters and it will convert it to feet (after hitting Convert).
Really, all I need is how to switch the positions (the math part is easy enough), but I just can't think of how the logic would work for that. Here's what I have so far:
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()
(the first and last frames are just space padding) Thanks in advance for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建一个变量来存储要转换的内容,并让
swap
函数更改该变量并更新标签。要更改标签的文本,您可以执行label['text'] = 'new text'
或label.configure(text='new text')
。这是对您的代码的有效修改:Create a variable that stores what you are converting from, and have the
swap
function change the variable and update the labels. To change the text of a label you can dolabel['text'] = 'new text'
orlabel.configure(text='new text')
. Here's a working modification of your code:带下标的表比将选择硬连接到代码中更具可扩展性。下面使用下标 t0 和 t1 来索引标签表(英尺、米)和换算系数表(.3048、3.2808)。如果您想创建其他转换,例如摄氏度到华氏度,您可以添加到表中而不更改代码。
其他一些需要注意的事项:
以下交换函数使用表/下标方法:
Tables with subscripts are more expandable than hardwiring choices into code. The following uses subscripts t0 and t1 to index a table of labels (feet, meters) and a table of conversion factors (.3048, 3.2808). If you wanted to create additional conversions such as centigrade to fahrenheit, you could add to the tables without changing the code.
A few other things to note:
The following swap function uses the table/subscript method: