python:为用户输入定义基本 tkinter 类,但在子类中添加标签:缺少位置参数 self

发布于 2025-01-10 17:27:18 字数 1607 浏览 2 评论 0原文

所以我把这段代码分为两个不同的模块。第一个称为“GUI”,我想在其中存储所有 TKINTER 代码,然后是另一个,我将其称为 GUI。

由于我将该基础用于其他模块,因此我正在考虑添加一个子类,该子类将添加 2 个带有文本的标签供用户阅读,并且不会干扰其他模块的基础 GUI。问题是它不起作用。

它告诉我“add_labels()”缺少 1 个必需的位置参数:“self”。

希望得到一些帮助。我正在复制下面的 2 个代码:

# GUIs module
import tkinter as tk

class pbt_GUI(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.title("Manual Input")
        self.resizable(0,0)
        # create widgets
        self.frame = tk.Frame(self, width=300, height=300)
        self.GUI_date = tk.Label(self.frame, text='Date (mm/dd/yyyy):').grid(row=0, sticky='w')
        self.GUI_date_input = tk.Entry(self.frame)
        self.submit = tk.Button(self.frame, text="Submit")
        # widgets layout
        self.frame.grid()
        self.GUI_date_input.grid(row=0, column=1)
        self.submit.grid(row=5)

        self.username = tk.Label(self.frame, text='username: bla')
        self.password = tk.Label(self.frame, text='password: bla**')

    def add_labels(self):
        self.username.grid(row=3)
        self.password.grid(row=4)

然后是其他模块:

# module where executed
from datetime import datetime
import generalUse.GUIs

date_input = ('')

def get_man_input():
    global date_input

    date_input = datetime.strptime(UI_GUI.GUI_date_input.get(), '%m/%d/%Y')
    date_input.strftime('%Y/%m/%d').date
    UI_GUI.destroy()


# Button set up for executing the GUI:
UI_GUI = generalUse.GUIs.pbt_GUI.add_labels()
UI_GUI.submit['command'] = get_man_input
UI_GUI.mainloop()

提前非常感谢

So I have this code divided between two different modules. First one, called 'GUIs' where I want to store all the TKINTER codes and then another from where I call the GUI.

Since I am using that base for other modules I was thinking about adding a subclass that would add 2 labels with text for the user to read and not disturb the base GUI for the other modules. The thing is that it is not working.

It tells me that 'add_labels()' is missing 1 required positional argument: 'self'.

Would appreciate some help. I am copying below the 2 codes:

# GUIs module
import tkinter as tk

class pbt_GUI(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.title("Manual Input")
        self.resizable(0,0)
        # create widgets
        self.frame = tk.Frame(self, width=300, height=300)
        self.GUI_date = tk.Label(self.frame, text='Date (mm/dd/yyyy):').grid(row=0, sticky='w')
        self.GUI_date_input = tk.Entry(self.frame)
        self.submit = tk.Button(self.frame, text="Submit")
        # widgets layout
        self.frame.grid()
        self.GUI_date_input.grid(row=0, column=1)
        self.submit.grid(row=5)

        self.username = tk.Label(self.frame, text='username: bla')
        self.password = tk.Label(self.frame, text='password: bla**')

    def add_labels(self):
        self.username.grid(row=3)
        self.password.grid(row=4)

And then there is the other module:

# module where executed
from datetime import datetime
import generalUse.GUIs

date_input = ('')

def get_man_input():
    global date_input

    date_input = datetime.strptime(UI_GUI.GUI_date_input.get(), '%m/%d/%Y')
    date_input.strftime('%Y/%m/%d').date
    UI_GUI.destroy()


# Button set up for executing the GUI:
UI_GUI = generalUse.GUIs.pbt_GUI.add_labels()
UI_GUI.submit['command'] = get_man_input
UI_GUI.mainloop()

Thank you very much in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清秋悲枫 2025-01-17 17:27:18

您需要创建一个 pbt_GUI 实例并使用该实例调用 add_labels()

...
UI_GUI = generalUse.GUIs.pbt_GUI() # create instance of pbt_GUI
UI_GUI.add_labels()
...

You need to create an instance of pbt_GUI and use this instance to call add_labels():

...
UI_GUI = generalUse.GUIs.pbt_GUI() # create instance of pbt_GUI
UI_GUI.add_labels()
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文