【问题标题】:Passing variable in classes/def and using commands in Tkinter在 classes/def 中传递变量并在 Tkinter 中使用命令
【发布时间】:2012-06-26 08:52:24
【问题描述】:

以下代码(未全部显示)是我第一次尝试使用类。我试图主要传递变量框,因为我希望从 Tkinter 中的 Entry 小部件中检索值。

我似乎已经完成了课程,但我在通过 Tkinter 小部件中的“命令”从内部调用定义时遇到了困难。

如果有人能解释我写得很糟糕的类代码哪里出了问题,以及是否有可能获得框值,我会非常高兴。

如果这是重复之前的任何问题,我深表歉意。

class Start():
    def ChoiceBox(self, choice):


        column = 0
        if choice == "Fixed":
            choice_frame.grid_forget()      
            tkMessageBox.showinfo("Message", "No optimisation, value fixed.")
        elif choice == "List":
            i = [0, 1, 2, 3]

            for i in i:
                choice_title = Label(choice_frame, text='Value %g'% float(i+1), bg='white', borderwidth=0, width=0)
                choice_title.grid(row=0, column=column+i, sticky="nsew", padx=1, pady=1)

                self.box = Entry(choice_frame, bg='white', borderwidth=0, width=0)
                self.box.grid(row=1, column=column+i, sticky="ew", padx=1, pady=1)

                tkMessageBox.showinfo("Message", "Please fill in list values.")


        elif choice == "Interval" or "Optimisation":
            i = [0, 1]
            choice_title1 = Label(choice_frame, text='Min Value', bg='white', borderwidth=0, width=0)
            choice_title1.grid(row=0, column=column, sticky="nsew", padx=1, pady=1)
            choice_title2 = Label(choice_frame, text='Max Value', bg='white', borderwidth=0, width=0)
            choice_title2.grid(row=0, column=column+1, sticky="nsew", padx=1, pady=1)
            for i in i:
                box = Entry(choice_frame, bg='white', borderwidth=0, width=0)
                box.grid(row=1, column=column+i, sticky="nsew", padx=1, pady=1)
                tkMessageBox.showinfo("Message", "Enter Min/Max values.")


    def StartBut(self):
    if self.box.get() == "":
            pass
        else:
            value = self.box.get()

list1 = OptionMenu(frame_table, variablelist, 'Fixed', 'List', 'Interval', 'Optimisation', command=Start.ChoiceBox)

but1 = Button(frame_but, text='Start', command=Start.StartBut)

更新:我现在得到的错误:'unbound method 'ChoiceBox()' must be called with Start instance 作为第一个参数(取而代之的是 str 实例)。

【问题讨论】:

    标签: python class tkinter ttk


    【解决方案1】:

    def ChoiceBox(choice): 这应该是def ChoiceBox(self, choice):

    阅读更多关于 python 类如何工作的信息here.

    【讨论】:

    • 感谢指正。我来到stackoverflow,因为我真的很难理解我找到的任何课程教程。稍后在与 Tkinter 相关的命令中,我无法从类中调用定义。
    • @user2063 几乎所有的 GUI 框架都需要 Classes 的基本知识,所以可能你应该先看一本书或看一些在线教程来了解你的基础知识。
    • 我知道,但是对于分配给我的任务,我没有时间,只需要弄清楚这个小问题。我很想理解它,因为在不知道自己在做什么的情况下不得不这样做让我很恼火。
    • 你能帮我把我的代码组织到类 init 中吗?如果这是我需要做的,我想尝试包括课程。
    猜你喜欢
    • 2018-09-21
    • 1970-01-01
    • 1970-01-01
    • 2013-06-26
    • 2021-01-01
    • 1970-01-01
    • 2011-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多