swapping in Tkinter
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 = Tkint开发者_如何学编程er.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!
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 do label['text'] = 'new text'
or label.configure(text='new text')
. Here's a working modification of your code:
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()
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 pythonic way to swap is to use compound assignment (a,b = b,a) rather than a temp variable (t=a, a=b, b=t).
- Anytime the same thing gets repeated many times (such as the font specification - WOW that's big and ugly!), consider making it a variable. It's both easier to change and more compact.
- There's no need to create blank frames for spacing, just use padding
- the grid layout makes the feet/meter labels/values line up cleanly.
The following swap function uses the table/subscript method:
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()
精彩评论