Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 动态创建combox并重置值TKinter_Python_Python 3.x_Tkinter - Fatal编程技术网

Python 动态创建combox并重置值TKinter

Python 动态创建combox并重置值TKinter,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,我动态生成组合框,并使用 列出值。 单击clearall按钮时,我想将所有combox框值设置为“无” 我尝试了很多方法。但它会显示错误消息“AttributeError:‘str’对象没有属性‘set’” 下面是我的代码 from tkinter import * from tkinter import messagebox as mb from tkinter import ttk from tkinter.ttk import Combobox import tkinter as tk

我动态生成组合框,并使用 列出值。 单击clearall按钮时,我想将所有combox框值设置为“无” 我尝试了很多方法。但它会显示错误消息“AttributeError:‘str’对象没有属性‘set’” 下面是我的代码

from tkinter import *
from tkinter import messagebox as mb 
from tkinter import ttk 
from tkinter.ttk import Combobox
import tkinter as tk 


root = Tk()
root.title("my application")
sizex = 500
sizey = 500
posx  = 50
posy  = 50
root.wm_geometry("%dx%d+%d+%d" % (sizex, sizey, posx, posy))

value1=['country1','country2','country3','country4','country5',"None"]
comblist =['combo_box1','combo_box2','combo_box3','combo_box4']

y2=80
for comb in comblist:        
    comb= ttk. Combobox(root, values=value1, state="readonly")
    comb.set("country1")
    comb.place(x=200,y=y2)

    y2=y2+30
    
def clearvalues(): 
    res = mb.askquestion('ClearValues',  "Want to clear all the setting ?")
    if res == 'yes' :         
        for cmb1 in comblist:   
             cmb1.set("None")
    else : 
        mb.showinfo('Return', 'Cancelled')                   

Button_clearall = Button(root,text="CLEARALL",command = clearvalues)
Button_clearall.place(x=100, y=10)
root.mainloop()

在函数
clearvalues():
中,您正在迭代
comblist
变量,而不是您创建的实际组合框。这就是为什么要返回
“AttributeError:'str'对象没有属性'set'”
。您正在遍历列表中的字符串

不建议通过迭代创建组合框,因为以后访问它们很困难。我建议创建5个单独的组合框并从那里开始

编辑:只是为了让你更清楚一点。如果将函数
clearvalues():
更改为如下所示:

def clearvalues():
    res = mb.askquestion('ClearValues', "Want to clear all the setting ?")
    if res == 'yes':
        for cmb1 in comblist:
            print(type(cmb1))
    else:
        mb.showinfo('Return', 'Cancelled')

现在运行程序并单击“是”清除所有设置。您将在终端中看到
cmb1
类型是字符串而不是组合框

您正在将
comblist
设置为字符串列表。如果要迭代组合框,则需要迭代组合框,而不是字符串

有很多方法可以做到这一点。在您的特定情况下,可能最简单的方法是去掉
comblist
的原始定义,并用组合框列表替换它

comblist = []
for i in range(4):        
    comb= ttk. Combobox(root, values=value1, state="readonly")
    comblist.append(comb)
    ...
这样,在
comblist
上迭代的代码就可以工作了:

 for cmb1 in comblist:   
     cmb1.set("None")

“不建议通过迭代创建组合框,因为以后访问它们很困难。”-这根本不是真的。通过将它们保存到列表或字典中,以后访问它们是很简单的。仅仅因为可以做一些事情并不意味着应该做。这是非常正确的,但在循环中创建小部件并不是其中之一。当您需要许多几乎相同的小部件时,这是减少重复代码的一种非常常见的技术。请帮助我完成上面的代码,如何使用loop@Continuit您应该始终遵循DRY-Don't Repeat Yourself的概念,以提高代码的效率,这里的循环是最好的方法,而且也不难理解。