0

我是python新手,没有任何经验就开始创建这段代码,当然我对编码知之甚少,我会犯很多愚蠢的错误,这种情况也不例外。我试图将选项菜单分配给我的列表,但我收到一个错误:

Traceback (most recent call last):
  File "main.py", line 91, in <module>
    app = CollegeApp()
  File "main.py", line 13, in __init__
    frame = F(self.container, self)
  File "main.py", line 70, in __init__
    self.userEntry()
  File "main.py", line 76, in userEntry
    indivSet = OptionMenu(self, self.usersInd, *Individuals)
TypeError: __init__() missing 1 required positional argument: 'value'

(据我所知,'value'列表应该包含一个项目吗?)

这个想法是用户在Entry Widget中输入名字,之后所有这些名字都存储在“个人”列表中。我希望这些名称显示在选项菜单中,并且能够从下拉列表中选择任何名称。非常重要的是,每个名称都可以与其他名称分开选择,以免选择整个名称列表。

事实上,我在 Google 中找到了关于如何将列表绑定到选项菜单的说明,但是有一个问题,在所有这些说明中,以已经填充项目的列表为例。在我的情况下,列表最初是空的,但逐渐填充了新项目(名称),我不知道哪些项目将在列表中,因为每个新用户输入都是唯一的

我想知道如何创建这样的下拉列表。

我的代码:

from tkinter import *
from tkinter import messagebox
import tkinter.ttk as ttk


class CollegeApp(Tk):
    def __init__(self):
        Tk.__init__(self)
        self.container = ttk.Frame(self)
        self.container.pack(side="top", fill="both", expand=True)
        self.frames = {}
        for F in (IndividPage ,counterPage):
            frame = F(self.container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")
        self.show_frame(IndividPage)
        self.lift()

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()


class IndividPage(ttk.Frame):

    def __init__(self, parent, controller):
        self.controller = controller
        ttk.Frame.__init__(self, parent)
        self.userEntry()

    def userEntry(self):
        headingTest = Label(self, text="Enter your UserName:", font="Arial 20")
        headingTest.grid(row=0, column=0, pady=5, padx=5)

        self.usernameEnter = Entry(self, width=40)
        self.usernameEnter.grid(row=0, column=1, padx=5, pady=5)

        confirmBtn = Button(self, text="Confirm User", font="Arial 16",
                            command=self.confirm)

        confirmBtn.config(height=4, width=12)
        confirmBtn.grid(row=2, column=2, sticky=E, padx=45, pady=360)

    def confirm(self):
        if self.add_to_indivList():
            pass

    def add_to_indivList(self):
        user = self.usernameEnter.get()
        if len(user) == 0:
            messagebox.showwarning(title='No user', message='Please enter a username!')
            return
        if self.usernameEnter.get():
            self.controller.show_frame(counterPage)

        if user in Individuals:
            messagebox.showwarning(title='In team', message=f'{user} is already in Individuals list!')

        Individuals.append(user)
        processedInd = list(dict.fromkeys(Individuals))
        self.controller.show_frame(counterPage)
        print(processedInd)
        print(len(Individuals))


class counterPage(ttk.Frame):
    def __init__(self, parent, controller):
        self.controller = controller
        ttk.Frame.__init__(self, parent)
        self.userEntry()

    def userEntry(self):

        self.usersInd = StringVar(self)
        self.usersInd.set(Individuals)
        indivSet = OptionMenu(self, self.usersInd, *Individuals)
        indivSet.grid(row=0, column=0, padx=10, pady=10)

        backBtn = Button(self, text="BACK", font="Arial 16", height=2, width=6,
                         command=lambda: self.controller.show_frame(IndividPage))
        backBtn.grid(row=1, column=0, sticky=W, pady=245, padx=10)


if __name__ == '__main__':
    Individuals = []
    app = CollegeApp()
    app.geometry("800x500")
    app.resizable(False, False)
    app.title('Points Counter')
    app.mainloop()
4

0 回答 0