猿问

在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())


我想要此代码执行的操作是希望将旋转框或缩放比例设置为数字n,然后单击“按钮”时,我希望显示n个子窗口。我使用while循环尝试了此操作,但在关闭prevoius窗口后立即创建一个新窗口,效果并不理想。您还必须先关闭主窗口才能正常工作(我将使按钮稍后自动关闭窗口)。关于如何进行这项工作的任何想法?


萧十郎
浏览 334回答 1
1回答
随时随地看视频慕课网APP

相关分类

Python
我要回答