python-3.x Toplevel类的面向对象构造函数(无副作用)

ogq8wdun  于 2023-03-31  发布在  Python
关注(0)|答案(1)|浏览(132)

我在编写一个继承自tkinter的Toplevel对话框的类的构造函数时遇到了麻烦。我从另一个本身继承自Frame的类调用它。我遇到的问题是我所提出的代码的一个有趣的副作用。当Toplevel对话框被示例化时,我得到了一个额外的调用Frame的示例。我只是想问一下,是否有人可以告诉我如何在自定义Toplevel类的构造函数中调用super。这是在Python框架中。我目前拥有的整个代码如下:

from tkinter import *
from tkinter import ttk

class App(Frame):

    dialog = None

    def __init__(self,  root):
        super(App, self).__init__(root)
        self.root = root

        self.create_widgets()

    def create_widgets(self):

        self.grid(row=3, column=1)
        self.t1 = Text(root, wrap="word")
        self.t1.grid(row=0, column=0, rowspan=2, sticky="nsew")
        self.b1 = Button(root, text=" Create ", command=self.show_dialog)
        self.b1.grid(row=2, column=0, pady=10)

        root.rowconfigure(0, weight=3)
        root.columnconfigure(0, weight=1)

    def show_dialog(self):

        self.dialog = Toplevel(root)
        self.dialog.title("Child")

        # Place child window over the application window
        x = self.root.winfo_x() + self.root.winfo_width()//2 - \
            self.dialog.winfo_width()//2
        y = self.root.winfo_y() + self.root.winfo_height()//2 - \
            self.dialog.winfo_height()//2
        self.dialog.geometry(f"100x75+{x}+{y}")

        Dialog(self.t1, self.dialog)

class Dialog(Toplevel):

    def __init__(self, t1, top):

        super().__init__(top)    # This doesn't work !!!

        self.top = top
        self.t1 = t1    # Text widget is passed at creation

        self.widget()

    def widget(self):
        self.e1 = Entry(self)
        self.e1.grid(row=0, column=0)
        self.b1 = Button(self, text="Submit", command=self.on_submit)
        self.b1.grid(row=1, column=0, pady=15)

        # self.rowconfigure(0, weight=1)

    def on_submit(self):
        self.t1.insert("insert", self.e1.get())

    def quit(self):
        self.top.destroy()

root = Tk()
root.title("Paremt")
root.geometry("400x250+400+200")

app = App(root)
app.mainloop()

我已经更新了这篇文章,展示了整个列表。我也试图从下面的评论中改进它。我仍然得到了副作用。现在它似乎示例化了两个顶级窗口,而不仅仅是类。

5ktev3wc

5ktev3wc1#

感谢所有的帮助,我在将Java中的对象知识应用到Python/Tk范式时遇到了困难。现在看起来一切都很好。更新的清单如下。

from tkinter import *
from tkinter import ttk

class App(Frame):

    def __init__(self,  root):
        super(App, self).__init__(root)

        self.create_widgets()

    def create_widgets(self):

        self.t1 = Text(root, wrap="word")
        self.t1.grid(row=0, column=0, rowspan=2, sticky="nsew")
        self.b1 = Button(root, text=" Create ", command=self.show_dialog)
        self.b1.grid(row=2, column=0, pady=10)

        root.rowconfigure(0, weight=3)
        root.columnconfigure(0, weight=1)

    def show_dialog(self):

        Dialog(root, self.t1)

    def quit(self):
        self.destroy()

class Dialog(Toplevel):

    def __init__(self, top, t1):

        super().__init__(top)

        self.t1 = t1    # Text widget is passed at creation
        self.title("Child")
        # Place child window over the application window
        x = root.winfo_x() + root.winfo_width()//2 - self.winfo_width()//2
        y = root.winfo_y() + root.winfo_height()//2 - self.winfo_height()//2
        self.geometry(f"100x75+{x}+{y}")

        self.widget()
        self.wait_visibility()
        self.transient(root)   # dialog window is related to main

    def widget(self):
        self.e1 = Entry(self)
        self.e1.grid(row=0, column=0)
        self.b1 = Button(self, text="Submit", command=self.on_submit)
        self.b1.grid(row=1, column=0, pady=15)

        root.rowconfigure(0, weight=1)
        root.columnconfigure(0, weight=1)   

    def on_submit(self):
        self.t1.insert("insert", self.e1.get())

    def quit(self):
        self.destroy()

root = Tk()
root.title("Paremt")
root.geometry("400x250+400+200")

app = App(root)
app.mainloop()

相关问题