如何通过循环制作多个窗口?

时间:2016-08-26 20:49:55

标签: python tkinter

我是一名初学者python编码器,我正在尝试创建一个GUI,您可以在其中输入多个学期的信息。一旦用户输入了学期数,我想单独询问每个学期,但是当我使用循环执行此操作时,所有窗口都会立即打开。有没有办法将它们放入序列中?

这是我到目前为止所拥有的

def createSemesterWin(self, numSemesters):
    for x in range(numSemesters):
        semesterWin = Toplevel()
        semesterg = SemesterGUI(semesterWin, x+1)
        semesterWin.mainloop

1 个答案:

答案 0 :(得分:0)

这样的事情可以帮助你顺利开始:

from Tkinter import *

## Define root and geometry
root = Tk()
root.geometry('200x200')

# Define Frames
win1, win2, win3 = Frame(root, bg='red'), Frame(root, bg='green'), Frame(root, bg='blue')

# Configure Rows
root.grid_rowconfigure(0, weight = 1)
root.grid_columnconfigure(0, weight = 1)

# Place Frames
for window in [win1, win2, win3]:
    window.grid(row=0, column = 0, sticky = 'news')

# Raises first window 'To the top'
win1.tkraise()

# Function to raise 'window' to the top
def raise_frame(window):
    window.tkraise()

# Page1 label / button
l1 = Label(win1, text = 'This is Page1').pack(side=TOP)
P1 = Button(win1, text = 'Next Page', command = lambda:raise_frame(win2)).pack(side=BOTTOM)

# Page2 label / button
l2 = Label(win2, text = 'This is Page2').pack(side=TOP)
p2 = Button(win2, text = 'Next Page', command = lambda:raise_frame(win3)).pack(side=BOTTOM)

# Page3 label / button
l3 = Label(win3, text = 'This is Page3').pack(side=TOP)
p3 = Button(win3, text = 'Next Page', command = lambda:raise_frame(win1)).pack(side=BOTTOM)

root.mainloop()

让我知道你的想法!