Python TkInter中Entry框的get方法

huangapple go评论101阅读模式
英文:

Python TkInter get method for entry box

问题

Here is the translated code snippet with the code parts excluded as you requested:

  1. class App(customtkinter.CTk):
  2. def __init__(self):
  3. super().__init__()
  4. self.geometry("500x300")
  5. self.title("Psychrometrics")
  6. customtkinter.set_appearance_mode("system")
  7. customtkinter.set_default_color_theme("green")
  8. label_title = customtkinter.CTkLabel(self, text="Saturation Pressure")
  9. label_title.grid(row=0, column=0, pady=DPadY, padx=DPadX)
  10. entry_db = customtkinter.CTkEntry(self, placeholder_text="Dry Bulb")
  11. entry_db.grid(row=1, column=0, pady=DPadY, padx=DPadX)
  12. label_dbsp = customtkinter.CTkEntry(self, width=300, placeholder_text="Dry Bulb Saturation Pressure")
  13. label_dbsp.grid(row=1, column=1, pady=DPadY, padx=DPadX)
  14. entry_wb = customtkinter.CTkEntry(self, placeholder_text="Wet Bulb")
  15. entry_wb.grid(row=2, column=0, pady=DPadY, padx=DPadX)
  16. label_wbsp = customtkinter.CTkEntry(self, width=300, placeholder_text="Wet Bulb Saturation Pressure")
  17. label_wbsp.grid(row=2, column=1, pady=DPadY, padx=DPadX)
  18. button = customtkinter.CTkButton(self, text="Calculate", command=login)
  19. button.grid(row=3, column=0, pady=DPadY, padx=DPadX)
  20. def get_db(self):
  21. _Db = self.entry_db.get()
  22. return _Db
  23. def login():
  24. Cdb = app.get_db()
  25. print(Cdb)
  26. app = App()
  27. 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'

  1. class App(customtkinter.CTk):
  2. def __init__(self):
  3. super().__init__()
  4. self.geometry("500x300")
  5. self.title("Psychrometrics")
  6. customtkinter.set_appearance_mode("system")
  7. customtkinter.set_default_color_theme("green")
  8. label_title = customtkinter.CTkLabel(self, text="Saturation Pressure")
  9. label_title.grid(row=0, column=0, pady=DPadY, padx=DPadX)
  10. entry_db = customtkinter.CTkEntry(self, placeholder_text="Dry Bulb")
  11. entry_db.grid(row=1, column=0, pady=DPadY, padx=DPadX)
  12. label_dbsp = customtkinter.CTkEntry(self, width=300, placeholder_text="Dry Bulb Saturation Pressure")
  13. label_dbsp.grid(row=1, column=1, pady=DPadY, padx=DPadX)
  14. entry_wb = customtkinter.CTkEntry(self, placeholder_text="Wet Bulb")
  15. entry_wb.grid(row=2, column=0, pady=DPadY, padx=DPadX)
  16. label_wbsp = customtkinter.CTkEntry(self, width=300, placeholder_text="Wet Bulb Saturation Pressure")
  17. label_wbsp.grid(row=2, column=1, pady=DPadY, padx=DPadX)
  18. button = customtkinter.CTkButton(self, text="Calculate", command=login)
  19. button.grid(row=3, column=0, pady=DPadY, padx=DPadX)
  20. def get_db(self):
  21. _Db = self.entry_db.get()
  22. return _Db
  23. def login():
  24. Cdb = app.get_db()
  25. print(Cdb)
  26. app = App()
  27. 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 方法中访问它。
然后你的代码将如下所示:

  1. class App(customtkinter.CTk):
  2. def __init__(self):
  3. super().__init__()
  4. self.geometry("500x300")
  5. self.title("Psychrometrics")
  6. customtkinter.set_appearance_mode("system")
  7. customtkinter.set_default_color_theme("green")
  8. label_title = customtkinter.CTkLabel(self, text="Saturation Pressure")
  9. label_title.grid(row=0, column=0, pady=DPadY, padx=DPadX)
  10. self.entry_db = customtkinter.CTkEntry(self, placeholder_text="Dry Bulb")
  11. self.entry_db.grid(row=1, column=0, pady=DPadY, padx=DPadX)
  12. label_dbsp = customtkinter.CTkEntry(self, width=300, placeholder_text="Dry Bulb Saturation Pressure")
  13. label_dbsp.grid(row=1, column=1, pady=DPadY, padx=DPadX)
  14. entry_wb = customtkinter.CTkEntry(self, placeholder_text="Wet Bulb")
  15. entry_wb.grid(row=2, column=0, pady=DPadY, padx=DPadX)
  16. label_wbsp = customtkinter.CTkEntry(self, width=300, placeholder_text="Wet Bulb Saturation Pressure")
  17. label_wbsp.grid(row=2, column=1, pady=DPadY, padx=DPadX)
  18. button = customtkinter.CTkButton(self, text="Calculate", command=login)
  19. button.grid(row=3, column=0, pady=DPadY, padx=DPadX)
  20. def get_db(self):
  21. _Db = self.entry_db.get()
  22. 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:

  1. class App(customtkinter.CTk):
  2. def __init__(self):
  3. super().__init__()
  4. self.geometry("500x300")
  5. self.title("Psychrometrics")
  6. customtkinter.set_appearance_mode("system")
  7. customtkinter.set_default_color_theme("green")
  8. label_title = customtkinter.CTkLabel(self, text="Saturation Pressure")
  9. label_title.grid(row=0, column=0, pady=DPadY, padx=DPadX)
  10. self.entry_db = customtkinter.CTkEntry(self, placeholder_text="Dry Bulb")
  11. self.entry_db.grid(row=1, column=0, pady=DPadY, padx=DPadX)
  12. label_dbsp = customtkinter.CTkEntry(self, width=300, placeholder_text="Dry Bulb Saturation Pressure")
  13. label_dbsp.grid(row=1, column=1, pady=DPadY, padx=DPadX)
  14. entry_wb = customtkinter.CTkEntry(self, placeholder_text="Wet Bulb")
  15. entry_wb.grid(row=2, column=0, pady=DPadY, padx=DPadX)
  16. label_wbsp = customtkinter.CTkEntry(self, width=300, placeholder_text="Wet Bulb Saturation Pressure")
  17. label_wbsp.grid(row=2, column=1, pady=DPadY, padx=DPadX)
  18. button = customtkinter.CTkButton(self, text="Calculate", command=login)
  19. button.grid(row=3, column=0, pady=DPadY, padx=DPadX)
  20. def get_db(self):
  21. _Db = self.entry_db.get()
  22. return _Db

Should work.
Do the same to all other entry boxes to be able to print or retrieve their contents.

huangapple
  • 本文由 发表于 2023年5月13日 19:14:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76242424.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定