您好,我目前正在使用4个组合框。当我检查组合框是否返回组合框的值时,它将引发tkinter异常。
我尝试使用Google搜索问题,并对代码进行了一些调整,但没有一个帮助:
import sys
import tkinter.messagebox as box
from tkinter.filedialog import asksaveasfile
if sys.version_info[0] >= 3:
import tkinter as tk
import tkinter.ttk as ttk
else:
import Tkinter as tk
class App(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
self.dict = {'Japan': ['Yokohama', 'Kobe', 'Tokyo'],
'China': ['Ningbo', 'Shanghai', 'Hefei'],
'USA': ['Baltimore', 'Long Beach', 'Chicago'],
'Russia': ['Moscow']}
self.i = ["EXW", "FOB", "DAT"]
self.c = ["40ton", "20ton", "Other"]
self.variable_c = tk.StringVar(self)
self.variable_i = tk.StringVar(self)
self.variable_a = tk.StringVar(self)
self.variable_b = tk.StringVar(self)
self.last_county = tk.StringVar(self)
self.area = tk.StringVar(self)
self.country = tk.StringVar(self)
self.incoterm = tk.StringVar(self)
self.capacity = tk.StringVar(self)
self.variable_b.trace('w', self.fun2)
self.variable_a.trace('w', self.update_options)
self.variable_i.trace('w', self.fun3)
self.variable_c.trace('w', self.fun4)
self.combobox_i = ttk.Combobox(self, values=list(self.i),
state='readonly')
self.combobox_i.bind("<<ComboboxSelected>>", self.fun3)
self.combobox_i.current(0)
self.combobox_c = ttk.Combobox(self, values=list(self.c),
state='readonly')
self.combobox_c.bind("<<ComboboxSelected>>", self.fun4)
self.combobox_c.current(0)
self.combobox_a = ttk.Combobox(self,
values=list(self.dict.keys()), state='readonly')
self.combobox_a.bind("<<ComboboxSelected>>", self.fun)
self.combobox_a.current(0)
self.combobox_b = ttk.Combobox(self, values=self.dict['Japan'],
state='readonly')
self.combobox_b.bind("<<ComboboxSelected>>", self.fun2)
self.combobox_b.current(0)
self.button = ttk.Button(self, text="Check", command=lambda :
sample(self.area, self.country, self.incoterm, self.capacity))
self.combobox_i.pack()
self.combobox_c.pack()
self.combobox_a.pack()
self.combobox_b.pack()
self.button.pack()
self.pack()
def fun(self,*args):
print("changed 1-st combobox value to: " + self.combobox_a.get())
if self.last_county != self.combobox_a.get():
self.combobox_b['values']=self.dict[self.combobox_a.get()]
self.combobox_b.current(0)
self.last_county = self.country = self.combobox_a.get()
return self.variable_a.get()
def fun2(self, *args):
print("changed 2-nd combobox value to" + self.combobox_b.get())
self.area = self.combobox_b.get()
return self.variable_b.get()
def fun3(self, *args):
print("changed 3-rd combobox value to" + self.combobox_i.get())
self.incoterm = self.combobox_i.get()
return self.variable_i.get()
def fun4(self, *args):
print("changed 4-tth combobox value to" + self.combobox_c.get())
self.capacity = self.combobox_c.get()
return self.variable_c.get()
def update_options(self, *args):
countries = self.dict[self.variable_a.get()]
self.variable_b.set(countries[0])
menu = self.combobox_b['menu']
menu.delete(0, 'end')
for country in countries:
menu.add_command(label=country, command=lambda
nation=country: self.variable_b.set(nation))
def sample(area, country, incoterm, capacity):
box.showinfo('info', 'Selected area: ' + area + '\nSelected country:
' + country + '\nSelected incoterm:' + incoterm
+ '\nSelected capacity' + capacity)
if __name__ == "__main__":
root = tk.Tk()
app = App(root)
app.mainloop()
当我运行程序并单击检查按钮时,它会显示:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\IDL Mongolia\AppData\Local\Programs\Python\Python37-
32\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "D:/IDLMongolia's/Desktop/Project/Count_Incoterms.py", line 57, in
<lambda>
self.button = ttk.Button(self, text="Check", command=lambda :
sample(self.area, self.country, self.incoterm, self.capacity))
File "D:/IDLMongolia's/Desktop/Project/Count_Incoterms.py", line 107, in
sample
+ '\nSelected capacity' + capacity)
TypeError: can only concatenate str (not "StringVar") to str
Process finished with exit code 0
请帮帮我。
答案 0 :(得分:1)
您正在尝试连接字符串和%
。要从StringVar
获取字符串,请使用StringVar
方法。
最简单的方法是将字符串传递给get
时调用get
。这样,样本就不需要了解sample
了。
StringVar
我会在代码的其他地方寻找问题,因为这些sample(self.area.get(), self.country.get(), self.incoterm.get(), self.capacity.get()))
变量之一是从字符串分配给的。如果您更改选项然后单击检查按钮,则会导致异常。
您的代码中肯定还有其他问题,但这可以解决问题中的那个问题。
答案 1 :(得分:1)
错误是不言自明的-您正在尝试将StringVar
对象与字符串连接起来。
通常,您需要在get
上使用StringVar
方法来检索其值。但是我注意到您实际上甚至不需要StringVar
,也没有以任何方式设置或更改其值。
要正确解决您的错误-只是不要首先创建StringVars
。而是将它们设置为空白字符串。
class App(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
...
self.area = ""
self.country = ""
self.incoterm = ""
self.capacity = ""
我真正建议的是根本不要创建虚拟变量。您始终可以直接通过“ combobox.get()”来检索值。
class App(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
...
#self.area = ""
#self.country = ""
#self.incoterm = ""
#self.capacity = ""
self.button = ttk.Button(self, text="Check", command=lambda : sample(self.combobox_a.get(), self.combobox_b.get(), self.combobox_i.get(), self.combobox_c.get()))
...