python-在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()

(第一帧和最后一帧只是空格填充)在此先感谢您的帮助!

最佳答案
创建一个变量,该变量存储要从中进行转换的内容,并让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()

转载注明原文:python-在Tkinter交换 - 代码日志