循环导入问题(类)- Tkinter Python

cmssoen2  于 2023-01-06  发布在  Python
关注(0)|答案(2)|浏览(107)

经过几个小时的尝试调试循环导入问题,我想在这里寻求一些帮助。
请注意,经过研究,我无法找到我的具体问题,这将是有帮助的。我发现的一些问题是基于类内的Button()。然而,这不是事实。我的目标是.get从文本框输入,并将其转换为json格式又名美化。(类似于可以在API测试工具中完成)
我在pointer.py中有一个函数可以完成这项工作,但是我想我应该在另一个文件中创建一个类来清理一下。

#--------------------------------------beautify_data.py--------------------------------------------#
import json
from pointer import text_box, payload_field

class Beautifier:
    def __init__(self, beautiful_payload):
        self.new = beautiful_payload

    def beautifying(self):
        json_object = json.loads(self.new) 
        new_json = json.dumps(json_object, indent=4) 
        text_box.delete('1.0', "end")  # Deleting content in text box
        text_box.insert('1.0', new_json) # Inserting pretty printed data
        payload_field.set(new_json) # Setting another field with pp data
        return new_json

#--------------------------------------pointer.py--------------------------------------------#

from beautify_data import Beautifier

button_save_payload = ttk.Button(width=8, text="BEAUTIFY", command=lambda: [Beautifier(get_text_box)])
button_save_payload.place(x=1110, y=75)

text_box = Text(window, width=68, height=29, bg="black", font=("Helvetica", 17))
text_box.grid(row=0, column=0, sticky="nsew")
text_box.place(x=420, y=70)
get_text_box = text_box.get("1.0", END)

mytest = Beautifier(get_text_box)
new = mytest.beautifying()
print(new)

我收到的错误是:

ImportError: cannot import name 'Beautifier' from partially initialized module 'beautify_data' (most likely due to a circular import)
9jyewag0

9jyewag01#

pointer中的代码不需要从pointer导入数据,而是需要给予对小部件的访问权限授予Beautifier类。您可以通过多种方式来实现这一点-您可以将数据传递给Beautifier.__init__,或者传递给beautifying。或者,您可以在pointer.py中创建一个更新其自己的小部件的函数,并将其作为回调传递。
下面是后者的一个示例,将回调传递给Beautifier.__init__

美化数据.py

class Beautifier:
    def __init__(self, beautiful_payload, callback=None):
        self.new = beautiful_payload
        self.callback = callback

    def beautifying(self):
        json_object = json.loads(self.new) 
        new_json = json.dumps(json_object, indent=4) 
        if self.callback:
            self.callback(new_json)
        return new_json

pointer.py

def update_ui(new_json):
    text_box.delete('1.0', "end")
    text_box.insert('1.0', new_json)
    payload_field.set(new_json)
...
mytest = Beautifier(get_text_box, update_ui)
new = mytest.beautifying()
...

我建议从Beautifier.__init__中删除beautiful_payload,并将其传递给beautifying,这样您就可以创建一个示例,然后多次使用它,而不必每次都创建一个新示例。

美化数据.py:

class Beautifier:
    def __init__(self, callback=None):
        self.callback = callback

    def beautifying(self, payload):
        json_object = json.loads(payload) 
        new_json = json.dumps(json_object, indent=4) 
        if self.callback:
            self.callback(new_json)
        return new_json

pointer.py

...
mytest = Beautifier(callback=update_ui)
get_text_box = text_box.get("1.0", END)
new = mytest.beautifying(get_text_box)
...
envsm3lx

envsm3lx2#

最好更新pointer.py中的小部件,这样就不需要在beautify_data.py中导入pointer.py

美化数据.py
import json

class Beautifier:
    def beautifying(self, txt):
        try:
            json_object = json.loads(txt)
            new_json = json.dumps(json_object, indent=4) 
            return new_json
        except json.decoder.JSONDecodeError as ex:
            print(ex)
pointer.py
from tkinter import *
from tkinter import ttk
from beautify_data import Beautifier

def beautify_json():
    get_text_box = text_box.get("1.0", "end-1c")
    mytest = Beautifier()
    result = mytest.beautifying(get_text_box)
    if result:
        text_box.delete("1.0", END)
        text_box.insert(END, result)

window = Tk()

text_box = Text(window, width=68, height=29, fg="white", bg="black", insertbackground="white", font=("Helvetica", 17))
text_box.grid(row=0, column=0, sticky="nsew")

button_save_payload = ttk.Button(width=8, text="BEAUTIFY", command=beautify_json)
button_save_payload.grid(row=1, column=0, sticky="e")

window.mainloop()

相关问题