在Python中重复GUIblock

2024-06-28 11:49:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试制作一个GUI,用户可以从列表框中选择一个值并存储它。用户将有多个选择,但每个列表框只有一个选择。目前,我有一个与nessecary属性列表框。它由以下代码生成:

from tkinter import *

class MyApp:
    def __init__(self, parent):
        self.myParent = parent #the root
        #select setup
        list = {"Setup 1","Blue Setup","Setup 12","Broken Setup","Noise Generator"}
        msg = "Select Setup"
        self.SVAL = 0;
        self.Sframe = Frame(parent)
        self.Sframe.pack()
        self.Slabel = Label(self.Sframe,text=msg)
        self.Slabel.pack()
        self.Sbox = Listbox(self.Sframe)
        for item in list:
            self.Sbox.insert(END,item)
        self.Sbox.pack()
        self.Sbut = Button(self.Sframe,text="select", command=self.Sselect)
        self.Sbut.pack()
        self.Smess = Message(self.Sframe,text="Go")
        self.Smess.pack()

    def Sselect(self):
        print(self.Sbox.curselection())
        self.SVAL = self.Sbox.curselection()
        self.Smess.configure(text=self.Sbox.get(self.SVAL[0]))


root = Tk()
myapp = MyApp(root)
root.mainloop()

我想在右边生成第二帧,但是如果我不必重复代码的话,那就太好了。目标是重复这个GUI块,只更改list和msg变量

list = {"Setup 1","Blue Setup","Setup 12","Broken Setup","Noise Generator"}
msg = "Select Setup"
self.S = generateblock(list,msg)
list = {"Joe","George","Harry"}
msg = "Select User"
self.U = generateblock(list,msg)

我希望这是可能的


Tags: textselfsetupguimsgrootselectpack
1条回答
网友
1楼 · 发布于 2024-06-28 11:49:41

您的“列表”不是列表,将产生错误。您还必须跟踪选择编号来自哪个列表框。一个简单的修改来显示这个概念

from tkinter import *
from functools import partial

class MyApp:
    def __init__(self, parent):
        self.myParent = parent #the root
        self.listbox_instances=[]
        #select setup
        list_of_choices = [["Setup 1","Blue Setup","Setup 12","Broken Setup",
                           "Noise Generator"],
                          ["test 2a", "test 2b", "test 2c"],
                          ["test 3a", "test 3b", "test 3c"],]
        msg = "Select Setup"
        ##self.SVAL = 0;
        for ctr in range(3):
            self.Sframe = Frame(parent)
            self.Sframe.grid(row=0, column=ctr)
            self.Slabel = Label(self.Sframe,text=msg)
            self.Slabel.pack()
            self.Sbox = Listbox(self.Sframe)
            for item in list_of_choices[ctr]:
                self.Sbox.insert(END,item)
            self.Sbox.pack()
            self.listbox_instances.append(self.Sbox)

            ## send the listbox number with the command call
            self.Sbut = Button(self.Sframe,text="select",
                               command=partial(self.Sselect, ctr))
            self.Sbut.pack()
            self.Smess = Message(self.Sframe,text="Go")
            self.Smess.pack()

    def Sselect(self, this_num):
        print("box_num=%d & item num=%s" % (this_num,
               self.listbox_instances[this_num].curselection()))
##        self.SVAL = self.Sbox.curselection()
##       self.Smess.configure(text=self.Sbox.get(self.SVAL[0]))


root = Tk()
myapp = MyApp(root)
root.mainloop()

相关问题 更多 >