英文:
Python TkInter get method for entry box
问题
Here is the translated code snippet with the code parts excluded as you requested:
class App(customtkinter.CTk):
def __init__(self):
super().__init__()
self.geometry("500x300")
self.title("Psychrometrics")
customtkinter.set_appearance_mode("system")
customtkinter.set_default_color_theme("green")
label_title = customtkinter.CTkLabel(self, text="Saturation Pressure")
label_title.grid(row=0, column=0, pady=DPadY, padx=DPadX)
entry_db = customtkinter.CTkEntry(self, placeholder_text="Dry Bulb")
entry_db.grid(row=1, column=0, pady=DPadY, padx=DPadX)
label_dbsp = customtkinter.CTkEntry(self, width=300, placeholder_text="Dry Bulb Saturation Pressure")
label_dbsp.grid(row=1, column=1, pady=DPadY, padx=DPadX)
entry_wb = customtkinter.CTkEntry(self, placeholder_text="Wet Bulb")
entry_wb.grid(row=2, column=0, pady=DPadY, padx=DPadX)
label_wbsp = customtkinter.CTkEntry(self, width=300, placeholder_text="Wet Bulb Saturation Pressure")
label_wbsp.grid(row=2, column=1, pady=DPadY, padx=DPadX)
button = customtkinter.CTkButton(self, text="Calculate", command=login)
button.grid(row=3, column=0, pady=DPadY, padx=DPadX)
def get_db(self):
_Db = self.entry_db.get()
return _Db
def login():
Cdb = app.get_db()
print(Cdb)
app = App()
app.mainloop()
Please note that the translation does not include the code parts and only includes the comments and text within the code.
英文:
I'm new to Python and trying to create an app to perform Psychrometric calculations. I have created a UI using customTkInter and want to retrieve the contents of an entry widget to perform some calculations. I have created a get method from within the class but when I try to call it the interpreter crashes saying there is no reference. From everything I have searched I appear to be doing it right but there must be something amiss. All help appreciated. Error is AttributeError: '_tkinter.tkapp' object has no attribute 'get_db'
class App(customtkinter.CTk):
def __init__(self):
super().__init__()
self.geometry("500x300")
self.title("Psychrometrics")
customtkinter.set_appearance_mode("system")
customtkinter.set_default_color_theme("green")
label_title = customtkinter.CTkLabel(self, text="Saturation Pressure")
label_title.grid(row=0, column=0, pady=DPadY, padx=DPadX)
entry_db = customtkinter.CTkEntry(self, placeholder_text="Dry Bulb")
entry_db.grid(row=1, column=0, pady=DPadY, padx=DPadX)
label_dbsp = customtkinter.CTkEntry(self, width=300, placeholder_text="Dry Bulb Saturation Pressure")
label_dbsp.grid(row=1, column=1, pady=DPadY, padx=DPadX)
entry_wb = customtkinter.CTkEntry(self, placeholder_text="Wet Bulb")
entry_wb.grid(row=2, column=0, pady=DPadY, padx=DPadX)
label_wbsp = customtkinter.CTkEntry(self, width=300, placeholder_text="Wet Bulb Saturation Pressure")
label_wbsp.grid(row=2, column=1, pady=DPadY, padx=DPadX)
button = customtkinter.CTkButton(self, text="Calculate", command=login)
button.grid(row=3, column=0, pady=DPadY, padx=DPadX)
def get_db(self):
_Db = self.entry_db.get()
return _Db
def login():
Cdb = app.get_db()
print(Cdb)
app = App()
app.mainloop()
Tried multiple ways of getting the value and simlply want to print to output the contents.
答案1
得分: 1
第一个问题是关于 def get_db(self):
方法的缩进。将其向后缩进,这样它就不会成为类的 __init__
的一部分。
其次,在 __init__
方法中,entry_db
应该改为 self.entry_db
,这样你也可以在 get_db
方法中访问它。
然后你的代码将如下所示:
class App(customtkinter.CTk):
def __init__(self):
super().__init__()
self.geometry("500x300")
self.title("Psychrometrics")
customtkinter.set_appearance_mode("system")
customtkinter.set_default_color_theme("green")
label_title = customtkinter.CTkLabel(self, text="Saturation Pressure")
label_title.grid(row=0, column=0, pady=DPadY, padx=DPadX)
self.entry_db = customtkinter.CTkEntry(self, placeholder_text="Dry Bulb")
self.entry_db.grid(row=1, column=0, pady=DPadY, padx=DPadX)
label_dbsp = customtkinter.CTkEntry(self, width=300, placeholder_text="Dry Bulb Saturation Pressure")
label_dbsp.grid(row=1, column=1, pady=DPadY, padx=DPadX)
entry_wb = customtkinter.CTkEntry(self, placeholder_text="Wet Bulb")
entry_wb.grid(row=2, column=0, pady=DPadY, padx=DPadX)
label_wbsp = customtkinter.CTkEntry(self, width=300, placeholder_text="Wet Bulb Saturation Pressure")
label_wbsp.grid(row=2, column=1, pady=DPadY, padx=DPadX)
button = customtkinter.CTkButton(self, text="Calculate", command=login)
button.grid(row=3, column=0, pady=DPadY, padx=DPadX)
def get_db(self):
_Db = self.entry_db.get()
return _Db
应该可以工作。对所有其他输入框执行相同的操作,以便能够打印或检索它们的内容。
英文:
The first issue is with the indentation of the def get_db(self):
method.
Indent it backward so it won't be part of the init of the class.
Secondly, in the init method, entry_db
should be self.entry_db
so you can access it inside the get_db
method as well.
Then your code will look like this:
class App(customtkinter.CTk):
def __init__(self):
super().__init__()
self.geometry("500x300")
self.title("Psychrometrics")
customtkinter.set_appearance_mode("system")
customtkinter.set_default_color_theme("green")
label_title = customtkinter.CTkLabel(self, text="Saturation Pressure")
label_title.grid(row=0, column=0, pady=DPadY, padx=DPadX)
self.entry_db = customtkinter.CTkEntry(self, placeholder_text="Dry Bulb")
self.entry_db.grid(row=1, column=0, pady=DPadY, padx=DPadX)
label_dbsp = customtkinter.CTkEntry(self, width=300, placeholder_text="Dry Bulb Saturation Pressure")
label_dbsp.grid(row=1, column=1, pady=DPadY, padx=DPadX)
entry_wb = customtkinter.CTkEntry(self, placeholder_text="Wet Bulb")
entry_wb.grid(row=2, column=0, pady=DPadY, padx=DPadX)
label_wbsp = customtkinter.CTkEntry(self, width=300, placeholder_text="Wet Bulb Saturation Pressure")
label_wbsp.grid(row=2, column=1, pady=DPadY, padx=DPadX)
button = customtkinter.CTkButton(self, text="Calculate", command=login)
button.grid(row=3, column=0, pady=DPadY, padx=DPadX)
def get_db(self):
_Db = self.entry_db.get()
return _Db
Should work.
Do the same to all other entry boxes to be able to print or retrieve their contents.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论