简体   繁体   English

如何让导入的模块访问 tkinter 小部件和变量?

[英]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.

相关问题 如何授予导入的库访问当前全局变量的权限? - How can I give an imported library access to current global variables? 如何在没有全局变量的情况下访问其他 Function 中定义的 Tkinter 小部件? - How to Access Tkinter Widgets Defined in Other Function Without Global Variables? 如果我在一个 function 中创建了小部件,我如何使用 PythonE460F8BEA5A9F5118 在另一个 function 中访问它们 - If I created widgets in one function, how can I access them in another function using Python Tkinter? 如何使同一列上的两个小部件在 Tkinter 中具有不同的宽度? - How can I make two widgets on the same column have different widths in Tkinter? 如何在不同模块导入时访问Python 2.7中的相对路径 - How can I access relative paths in Python 2.7 when imported by different modules 如何避免全局变量更改 Tkinter 中的小部件 - How to avoid global variables to change widgets in Tkinter 如何在Tkinter(Python)中将绑定添加到小部件的动态列表 - How can I add binds to a dynamic list of widgets in Tkinter (Python) Tkinter - 如何删除 ComboBox 小部件中的粗体文本和焦点? - Tkinter - How can I delete the bold text and the focus in the ComboBox widgets? Tkinter - 如何在 ScrolledText 小部件中添加边框? - Tkinter - How can I add a border in the ScrolledText widgets? 我如何销毁多个条目窗口小部件Tkinter Python - How can I destroy multiple entry widgets tkinter python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM