【问题标题】:How To Multiply / Divide Entry Box With A Number, tkinter如何用数字乘/除输入框,tkinter
【发布时间】: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


    【解决方案1】:

    您在这里遇到的问题是您试图将temp(一个字符串)除以dist(一个浮点数)。在使用float(temp) 转换之前,您需要将temp 转换为浮点数。此外,由于您在脚本期间设置了一次temp,因此它只会引用分配变量后输入框中的值。为了解决这个问题,您必须在 convert() 函数内部而不是外部调用 dentry.get()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-23
      • 1970-01-01
      • 1970-01-01
      • 2021-05-25
      相关资源
      最近更新 更多