【发布时间】:2019-11-07 14:21:48
【问题描述】:
我做了这两组不同的代码,它们在 tkinter 上执行相同的功能。 simplediaglog 版本工作得很好,但我想使用 entry(因为我想在同一个弹出窗口上显示输出),这不起作用。
问题主要在于将字典中的浮点值列表与条目输入值相乘,并让python甚至将条目输入识别为int。
我尝试重组 try-except-else,尝试使用不同的方法使用 IntVar() 将条目输入转换为整数。我还尝试索引字典,即 x[y][0]。我尝试将浮点值放入字符串中,例如x = {“A”:“2.3”,“B”:“0.3”}。但是没有任何效果,不断收到不同的错误,但归结为一件事:无法将整数/str 与字典中的浮点值相乘。
def p(y):
while True:
Q = simpledialog.askstring("test", "Enter number:")
try:
Q = int(Q)
if Q is None:
break
elif Q <=0 or Q> 10:
raise ValueError
else:
W = str(round(Q * x[y], 1)) # Round off to 1 d.p
messagebox.showinfo("test", "Ans is\n" + W + " minute(s)")
break
except:
messagebox.showerror("Error", "enter a value from 0 to 10")
break
此代码有效并正确显示了所有内容。
顺便说一句,对于下面的代码,位置几何是我随机放置的,以查看整体代码是否有效,我认为错误不是由于几何。
上面的simpledialog代码有效。
但是,此条目小部件代码没有:
def p(y):
root = Tk()
root.title("Average Waiting Time")
root.geometry("800x500")
Label(root, text="Enter number of people: ").place(x=20, y=30)
Q = Entry(root, bd =5)
Q.place(x=220, y=30)
def B():
Q_input = Q.get()
while True:
try:
int(Q_input)
if Q_input is None:
continue
elif Q_input <=0 or Q_input > 10:
raise ValueError
else:
W = str(round(Q_input * x[y], 1)) # Round off to 1 d.p
Label(root, "Ans\n" + W + " minute(s)").place(x=50, y=500)
except:
messagebox.showerror("Error", "enter a value from 0 to 10")
break
ok = Button (root, text="OK", command = B)
ok.place(x=300, y=300)
cancel = Button (root, text="Cancel", command = root.destroy)
cancel.place(x=400,y=300)
root.mainloop()
这是我的浮点值列表字典:
x = {"A": 0.8, "B": 2.5, "C": 1.2}
我希望看到一个带有 Ans 2.3 minute(s) 之类的标签出现在与条目小部件相同的窗口中。
但即使我在输入输入中键入一个 int,例如 '5',try-except 也不会将其转换为 int,因为我不断收到“错误”消息:“输入一个从 0 到 10 的值” .
为什么会这样?是因为它是一个条目小部件而不是 simpledialog,因为它适用于 simplediaglog?
此外,即使我到达 W 行(设法将 Q_input 转换为整数),它也会显示错误:“str”对象没有属性“items”。
我的主要问题是为什么浮点字典列表(以及从 str 到 int 的类型转换)能够注册 simpledialog 但不能注册 entry 小部件?有什么区别?是否可以将字典中的浮点值与条目输入中的整数相乘?
另外,除了入口小部件,还有其他方法可以在同一个弹出窗口上显示输出吗?
提前谢谢你!
编辑: 所以,我尝试在 while 循环之前将 Q_input 转换为 int 并打印它。 如果我输入“5”,它会打印“5”。我知道它确实转换为 int,因为当我输入“hi”时,出现错误:int() 以 10 为底的无效文字:“hi”。
我也将int(Q_input) 更改为Q_input=int(Q_input)。根据建议,我还在if Q_input is None: 下将continue 更改为break。
但是,try-except 仍然不起作用,它甚至没有尝试将 Q_input 转换为 int,它只是直接跳到 except。
很抱歉问了这么多问题,所以我将其总结为一个问题:
我的try-except 代码是否因为 simpledialog 和 entry 小部件之间的差异而无法工作,如果是,那是什么原因,如果不是,那为什么代码不起作用?
解决方案:
我意识到了一些事情,因为这个原因我一直无法显示标签:Label(root, "Ans\n" + W + " minute(s)").place(x=50, y=500)。我没有放text=,哈哈我加text=后就可以成功显示标签了。我的错,所以一直以来的问题是没有将输入输入转换为整数。不过需要将int(Q_input) 更改为Q_input=int(Q_input)。我只是直接得出结论,错误是由于类型转换,因为程序一直直接转到except。
【问题讨论】:
-
我投了反对票,因为似乎有too much code 和too much information 以及太多问题合二为一。请重新调整您的帖子的重点:)
-
我投了赞成票,因为如果你真的阅读了这篇文章,这很有意义。你能在进入 While True 循环之前打印出 Q_input 的值并将 Q_input 转换为 int 吗?也打印出来看看你会得到什么。
-
@Yepram Yeransian 感谢您的建议。我在
while True循环之前转换并打印了 Q_input。如果我输入“5”,它会打印“5”。我知道它确实转换为 int,因为当我输入“hi”时,出现错误。但是,try-except仍然不起作用,一切都转到except部分。 -
@Corentin Pane 嗨,很抱歉让它看起来很乱,但我发布了这么多代码,因为我真正想问的问题是:“我的 try-except 代码是否因为 simpledialog 之间的差异而不起作用和入口小部件,如果是,那是什么原因,如果不是,那为什么代码不起作用?”。所以,我有点需要发布这两个代码,因为我在 simpledialog 和 entry 小部件之间进行比较。我只是为这个问题创建了一个帐户,因为我真的无法弄清楚。如果我做了一些超出协议的事情,我很抱歉 - 我想回复或编辑我的问题 - 对于这个网站。
-
当执行 elif 子句时 Q_input 是一个字符串
Q_input <=0 or Q_input > 10在 try: 块中引发TypeError: '<=' not supported between instances of 'str' and 'int'。因此它运行 except: 块并打印错误消息。
标签: python dictionary tkinter type-conversion try-except