1

我正在编写一个程序,我试图在其中打开多个窗口。我的代码是:

from tkinter import *
from tkinter import ttk

class Main_window(ttk.Frame):
    """A program"""
    def __init__(self, master):
        ttk.Frame.__init__(self, master)
        self.grid()
        self.create_widgets()

def create_widgets(self):
    """Creates all the objects in the window"""


    self.min_lbl = ttk.Label(self, text = "1").grid(row = 0, column = 0,
                                                    sticky = W)
    self.max_lbl = ttk.Label(self, text = "100").grid(row = 0, column = 2,
                                                    sticky = W)

    spinval = IntVar()

    self.scale = ttk.Scale(self, orient = HORIZONTAL,
                                   length = 200,
                                   from_ = 1, to = 100,
                                   variable = spinval,
                                   command=self.accept_whole_number_only)
    self.scale.grid(row = 0,column = 1,sticky = W)


    self.spinbox = Spinbox(self, from_ = 1, to = 100,
                                   textvariable = spinval,
                                   command = self.update,
                                   width = 10)
    self.spinbox.grid(row = 0,column =3,sticky = W)


    self.go_bttn = ttk.Button(self, text = "Go",
                              command = self.create_windows
                              ).grid(row = 1, column = 1, sticky = W)


def accept_whole_number_only(self, e=None):
    """Makes the numbers from the scale whole"""
    value = self.scale.get()
    if int(value) != value:
        self.scale.set(round(value))

def update(self):
    """Updates the scale and spinbox"""
    self.scale.set(self.spinbox.get())


def create_windows(self):
    """This function will create all the new windows"""
    value = self.scale.get()
    window_num = value
    negative_window_num = 1
    while window_num != 0:
        root = Tk()
        root.title("This is Window "+str(window_num)[:-2]+" of "+str(value)[:-2])
        root.geometry("350x200")
        app = Window_creator(root)
        root.mainloop()
        window_num -= 1


class Window_creator(ttk.Frame):
    """makes child windows"""
    def __init__(self, master):
        ttk.Frame.__init__(self, master)
        self.grid()
        self.create_widgets()

def create_widgets(self):
    """creates all the widgets in the window"""


def main():
    """Loops the window"""
    root = Tk()
    root.title("Programm")
    root.geometry("350x200")
    app = Main_window(root)
    root.mainloop()

main()

我想要这段代码做的是我希望能够将旋转框或比例设置为数字 n,然后当我单击按钮时,我希望出现 n 个子窗口。我用一个while循环尝试了这个,但它并没有像我想要的那样工作,在我关闭prevoius窗口之后创建一个新窗口。您还必须先关闭主窗口才能使其工作(稍后我将让按钮自动关闭窗口)。关于如何完成这项工作的任何想法?

4

1 回答 1

1

调用child = Toplevel(),而不是root = Tk(). 此外,您不能mainloop多次调用(因为应该只有一个事件循环)。

from tkinter import *
from tkinter import ttk

class Main_window(ttk.Frame):
    """A program"""
    def __init__(self, master):
        ttk.Frame.__init__(self, master)
        self.grid()
        self.create_widgets()

    def create_widgets(self):
        """Creates all the objects in the window"""


        self.min_lbl = ttk.Label(self, text = "1").grid(row = 0, column = 0,
                                                        sticky = W)
        self.max_lbl = ttk.Label(self, text = "100").grid(row = 0, column = 2,
                                                        sticky = W)

        spinval = IntVar()

        self.scale = ttk.Scale(self, orient = HORIZONTAL,
                                       length = 200,
                                       from_ = 1, to = 100,
                                       variable = spinval,
                                       command=self.accept_whole_number_only)
        self.scale.grid(row = 0,column = 1,sticky = W)


        self.spinbox = Spinbox(self, from_ = 1, to = 100,
                                       textvariable = spinval,
                                       command = self.update,
                                       width = 10)
        self.spinbox.grid(row = 0,column =3,sticky = W)


        self.go_bttn = ttk.Button(self, text = "Go",
                                  command = self.create_windows
                                  ).grid(row = 1, column = 1, sticky = W)


    def accept_whole_number_only(self, e=None):
        """Makes the numbers from the scale whole"""
        value = self.scale.get()
        if int(value) != value:
            self.scale.set(round(value))

    def update(self):
        """Updates the scale and spinbox"""
        self.scale.set(self.spinbox.get())


    def create_windows(self):
        """This function will create all the new windows"""
        value = self.scale.get()
        window_num = value
        negative_window_num = 1
        for n in range(int(window_num)):
            child = Toplevel()
            child.title("This is Window "+str(window_num)[:-2]+" of "+str(value)[:-2])
            child.geometry("350x200")
            app = Window_creator(child)


class Window_creator(ttk.Frame):
    """makes child windows"""
    def __init__(self, master):
        ttk.Frame.__init__(self, master)
        self.grid()
        self.create_widgets()

    def create_widgets(self):
        """creates all the widgets in the window"""


def main():
    """Loops the window"""
    root = Tk()
    root.title("Programm")
    root.geometry("350x200")
    app = Main_window(root)
    root.mainloop()

main()
于 2013-06-10T17:19:29.663 回答