python:为用户输入定义基本 tkinter 类,但在子类中添加标签:缺少位置参数 self
所以我把这段代码分为两个不同的模块。第一个称为“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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要创建一个
pbt_GUI
实例并使用该实例调用add_labels()
:You need to create an instance of
pbt_GUI
and use this instance to calladd_labels()
: