合并两个窗口,其中第二个窗口取决于第一个窗口

时间:2019-04-09 06:51:33

标签: python python-3.x tkinter

第一个窗口用于显示矩形的数量。它定义了第二个窗口中输入框的数量。我想制作一个带有要输入的矩形数的窗口,然后单击“提交”按钮时,第二个窗口的内容应出现在矩形数和“提交”按钮的下方,这样我就可以看到输入的矩形数。基本上,试图使所有内容仅出现在一个窗口中。 注意:该代码是可验证的。

from tkinter import *

rectangle_values = []
k = 0

def open_window():
    window.withdraw()
    global k
    top = Toplevel()
    top.title("Rectangles")

    for i in range(0, int(rectangles.get()) * 2):
        if (i % 2) == 0:
            l4 = Label(top, text="Size of rectangle:")
            l4.grid(row=i, column=0)
        en = Entry(top)
        en.grid(row=i, column=1)

    b4 = Button(top, text="Back", width=12, command=lambda: (top.destroy(), window.deiconify()))
    b4.grid(row=int(rectangles.get()) * 2 + 1, column=1)
    k = int(rectangles.get())

window = Tk()

l3 = Label(window, text="Number of Rectangles:")
l3.grid(row=0, column=0)

# defining entries
rectangles = StringVar()
e3 = Entry(window, textvariable=rectangles)
e3.grid(row=0, column=1)

# Defining buttons
b1 = Button(window, text='Submit', width=12, command=open_window)
b1.grid(row=3, column=1)

window.title("Rectangle Configuration")
window.mainloop()

2 个答案:

答案 0 :(得分:1)

您可以使用Frame小部件在一个窗口中完成所有操作。

from tkinter import *

rectangle_values = []
k = 0

def open_window(rectangles):
    global k
    window.title('Rectangles')
    print(rectangles)
    for i in range(0, int(rectangles) * 2):
        if (i % 2) == 0:
            l4 = Label(frame2, text="Size of rectangle:")
            l4.grid(row=i, column=0)
        en = Entry(frame2)
        en.grid(row=i, column=1)

    frame2.tkraise()

    b4 = Button(frame2, text="Back", width=12, command=lambda: (frame1.tkraise(), window.title('Rectangle Configuration')))
    b4.grid(row=int(rectangles) * 2 + 1, column=1)
    k = int(rectangles)

window = Tk()

frame1 = Frame(window)
frame1.grid(row = 0, column = 0, sticky = 'nsew')
frame2 = Frame(window)
frame2.grid(row = 0, column = 0, sticky = 'nsew')

frame1.tkraise()

l3 = Label(frame1, text="Number of Rectangles:")
l3.grid(row=0, column=0)

# defining entries
rectangles = StringVar()
e3 = Entry(frame1, textvariable=rectangles)
e3.grid(row=0, column=1)

# Defining buttons
b1 = Button(frame1, text='Submit', width=12, command=lambda: open_window(rectangles.get()))
b1.grid(row=3, column=1)

window.title("Rectangle Configuration")
window.mainloop()

答案 1 :(得分:1)

您可以将第一个“窗口”放置在一个框架中,然后将第二个“窗口”放置在一个框架中,然后在验证矩形数量后创建第二个“窗口”:

注意:我添加了默认值,以使验证和清空条目不会引发错误,并在验证其他条目时重置第二帧。

import tkinter as tk


class NumRectFrame(tk.Frame):

    def __init__(self, master):
        super().__init__(master)
        self.master = master
        self.label = tk.Label(self, text="Number of Rectangles:")
        self.label.grid(row=0, column=0)

        self.num_rectangles = tk.IntVar()
        self.num_rectangles_entry = tk.Entry(self, textvariable=self.num_rectangles)
        self.num_rectangles_entry.grid(row=0, column=1)
        self.num_rectangles_entry.insert(tk.END, 0)

        self.btn = tk.Button(self, text='validate', 
                             command=lambda: self.master.get_num_rect(int(self.num_rectangles_entry.get())))
        self.btn.grid(row=1, column=1)


class RectFrame(tk.Frame):

    def __init__(self, master, num_rect=0):
        super().__init__(master)
        self.master = master
        self.num_rect = num_rect
        for idx in range(self.num_rect * 2):
            if idx % 2 == 0:
                tk.Label(self, text="Size of rectangle:").grid(row=idx, column=0)
            tk.Entry(self).grid(row=idx, column=1)


class App(tk.Tk):

    def __init__(self):
        super().__init__()
        self.title('Num Rectangles')
        self.num_rect_fm = NumRectFrame(self)
        self.num_rect_fm.pack()

    def get_num_rect(self, val=0):
        try:
            self.rect_fm.destroy()
        except AttributeError:
            pass
        self.rect_fm = RectFrame(self, num_rect=val)
        self.rect_fm.pack()


app = App()
app.mainloop()