python - 在 tkinter 中创建 n 个窗口

标签 python tkinter

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

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 循环尝试了这个,但它并没有像我希望的那样工作,在我关闭前一个窗口后创建一个新窗口。您还必须先关闭主窗 Eloquent 能使其正常工作(稍后我将让按钮自动关闭窗口)。关于如何完成这项工作有什么想法吗?

最佳答案

调用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()

关于python - 在 tkinter 中创建 n 个窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17029304/

相关文章:

python - 为什么我会收到 TypeError : '<' not supported between instances of 'int' and 'tuple' ?

python - 在ggplot中为不同的分类级别绘制不同的颜色

python - 错误异常应继承基本Python错误对象

python - 如何在 Tkinter 中将图像 (.png) 放置在 `LabelFrame` 中并调整其大小?

python - 在 python 中单击时更改单击 tkinter 矩形的颜色

python - 如何在 Pyspark 中将行分成多行

python - Unicode 编码错误 : 'ascii' codec can't encode character u'\u2019'

python - 通过鼠标单击获取 matplotlib 绘图图 python 的坐标

python - ** 或 pow() 不支持的操作数类型 : 'Entry' and 'int' ?

python - 如何修复 "- _tkinter.TclError: no events specified in binding"