[英]How can I have imported modules access tkinter widgets and variables?
I had this Cafe Management System whom I've separated the gui and functions into modules.我有这个咖啡馆管理系统,我将 gui 和功能分成模块。
Here are some snippets:以下是一些片段:
main.py主文件
from tkinter import *
import checkbox_operation
import receipt_operation
class cafemanagementsystem:
def __init__(self, cms):
self.cms = cms
cms.title("Cafe Management System")
self.b1var = IntVar(value=1)
self.b1v = StringVar()
self.b1v.set("0")
self.b1 = Checkbutton(self.bevmenu, command=self.check, text="Latte", variable=self.b1var, onvalue=1, offvalue=0).grid()
self.b1a = Entry(self.bevmenu, bd=2, textvariable=self.b1v)
self.b1a.grid()
self.rcpt = Text(self.rcptmain, width=50, height=30, bd=4)
self.rcpt.grid()
self.btnrcpt = Button(self.rcptbtn, command=receipt_operation.receipt, text="Receipt").grid()
self.btnrst = Button(self.rcptbtn, command=receipt_operation.reset, text="Reset").grid()
def check(self):
checkbox_operation.check(self)
def receipt(self):
receipt_operation.receipt(self)
checkbox_operation.py复选框操作.py
def check(cafemanagementsytem_inst):
if b1var.get() == 1:
b1a.config(state=NORMAL)
elif b1var.get() == 0:
b1a.config(state=DISABLED)
b1v.set("0")
receipt_operation.py收据操作.py
def receipt():
rcpt.insert(END, "Offical Receipt\n")
rcpt.insert(END, "Latte \t\t\t\t" + b1v.get() + "\n")
My problem is that I can't get check()
and receipt()
to work.我的问题是我无法让check()
和receipt()
工作。 Also, def check(self) and def receipt(self)
gives the following error:此外, def check(self) and def receipt(self)
给出以下错误:
TypeError: check()/receipt() takes 0 positional arguments but 1 was given
Are there any solutions for this?有什么解决方案吗? Also, please tell me if the way I wrote the code contributed to the problem as I've been stuck in this problem for almost a week.另外,请告诉我我编写代码的方式是否导致了这个问题,因为我已经被这个问题困了将近一个星期。
You need to pass the proper variables into those other functions.您需要将适当的变量传递给其他函数。 I'm wondering why you have checkbox_operation
in a separate file, when it should be part of the class.我想知道为什么你在一个单独的文件中有checkbox_operation
,它应该是 class 的一部分。 It's not good practice that those external functions need to have such detailed knowledge of the internal workings of the class.这些外部功能需要对 class 的内部工作有如此详细的了解,这不是一个好的做法。
...
def check(self):
checkbox_operation.check(self)
def receipt(self):
receipt_operation.receipt(self)
def check(cafe):
if cafe.b1var.get() == 1:
cafe.b1a.config(state=NORMAL)
elif cafe.b1var.get() == 0:
cafe.b1a.config(state=DISABLED)
cafe.b1v.set("0")
def receipt(cafe):
cafe.rcpt.insert(END, "Offical Receipt\n")
cafe.rcpt.insert(END, "Latte \t\t\t\t" + cafe.b1v.get() + "\n")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.