在 Tkinter 中交换

发布于 2024-12-08 17:38:44 字数 1789 浏览 0 评论 0原文

我有一个简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

颜漓半夏 2024-12-15 17:38:44

创建一个变量来存储要转换的内容,并让 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()

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()
网白 2024-12-15 17:38:44

带下标的表比将选择硬连接到代码中更具可扩展性。下面使用下标 t0 和 t1 来索引标签表(英尺、米)和换算系数表(.3048、3.2808)。如果您想创建其他转换,例如摄氏度到华氏度,您可以添加到表中而不更改代码。

其他一些需要注意的事项:

  • Pythonic 的交换方式是使用复合赋值 (a,b = b,a) 而不是临时变量 (t=a, a=b, b=t)。
  • 任何时候相同的事情重复多次(例如字体规范 - 哇,又大又丑!),请考虑将其设为变量。它既更容易改变,也更紧凑。
  • 无需创建空白框架来进行间距,只需使用填充
  • 网格布局即可使英尺/米标签/值整齐排列。

以下交换函数使用表/下标方法:

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

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文