【发布时间】:2022-01-03 22:14:27
【问题描述】:
嘿,我对 python 很陌生,我在高中上课。对于我正在处理的任务,我们必须制作一个 tkinter gui,其中包括小部件、标签、按钮、输入框、单选或复选按钮、框架和显示框。我正在制作公里到英里的转换器,如果选择它可以做相反的事情,用户可以选择单选按钮来选择他们想要计算的那个,但是我在计算部分遇到了很多麻烦,因为我无法将输入框编号乘以或除以 1.609 或任何数字。 这是我现在拥有的代码,对于混乱以及它可能有多糟糕,我深表歉意
import tkinter as tk
import tkinter.messagebox
# Customize main window
root = tk.Tk()
root.title('GUI Assignment')
# Create the frames, right, left, and bottom, and pack them
bframe = tk.LabelFrame(root, highlightthickness=0, borderwidth=0, padx=100, pady=50)
bframe.pack(side='bottom')
rframe = tk.LabelFrame(root, highlightthickness=0, borderwidth=0, padx=100, pady=50)
rframe.pack(side='right')
lframe = tk.LabelFrame(root, highlightthickness=0, borderwidth=0, padx=100, pady=50)
lframe.pack(side='left')
# Make the label and the entry box for the right frame
dlabel = tk.Label(rframe, text = 'Enter the distance', )
dentry = tk.Entry(rframe, width=75)
# Pack the label and entry
dlabel.pack(side='left')
dentry.pack(side="right")
dist = 3.0
temp =(dentry.get())
# Create the convert command
def convert():
if radio_var.get()==1:
tkinter.messagebox.showinfo('Distance',temp / dist )
# Make the convert and quit buttons
b = tk.Button(bframe, text="Convert", command=convert)
quit = tk.Button(bframe, text='Quit', command=root.destroy)
# Pack the buttons
b.pack(side='left')
quit.pack(side='right')
# Make the radio variable
radio_var = tk.IntVar()
radio_var.set(1)
# Make the radio buttons for the left frame
rb = tk.Radiobutton(lframe, text='Km to Miles', variable=radio_var, value=1)
rb2 = tk.Radiobutton(lframe, text='Miles to Km', variable=radio_var, value=2)
# Pack The Radio Buttons
rb.pack()
rb2.pack()
root.mainloop()
【问题讨论】:
标签: python tkinter tkinter-entry