TkInter:如何在第二个窗口而不是第一个窗口上显示对象?

时间:2017-10-18 12:04:28

标签: python tkinter screenshot

from tkinter import *

def new_member():

    #This is for the larger window
    new_member_window = Tk()
    new_member_window.title("Add a new member")
    new_member_window.geometry("500x500")

    #NAME ENTRY

    name_entry_label = Label(text = "Enter a new member's name:")
    name_entry_label.place(x=7,y=-25,width=80,height=83)

    name_box = Entry(text="")
    name_box.place(x=100,y=10,width=100,height=15)

    #AGE ENTRY

    age_entry_label = Label(text = "Enter their age:")
    age_entry_label.place(x=7,y=10,width=80,height=83)

    age_box = Entry(text="")
    age_box.place(x=100,y=45,width=100,height=15)

    #EMAIL ADRESS ENTRY

    email_entry_label = Label(text = "Enter their email:")
    email_entry_label.place(x=10,y=45,width=80,height=83)

    age_box = Entry(text="")
    age_box.place(x=100,y=80,width=100,height=15)

    new_member_window.mainloop()

#All below is for the smaller window
menu_window = Tk()
menu_window.title("Sports Club Membership")
menu_window.geometry("264x164")

menu_label=Label(text="Main menu",font=("Helvetica",20,"underline","bold"))

menu_label.place(x=10,y=10)

#ADD A NEW MEMBER BUTTON

menu_new_member_button=Button(text="Add a new member",command=new_member)
menu_new_member_button.place(x=10,y=70,width=120,height=20)

#SEARCH MEMBER JOINING DATES

search_member_join_dates_button=Button(text="Search member joining dates")#,command=member_joining_dates)
search_member_join_dates_button.place(x=10,y=100,width=170,height=20)

#SEARCH FOR OVERDUE MEMBERSHIP PAYMENTS

search_overdue_membership_payments_button=Button(text="Search for overdue membership payments")#,command=overdue_membership_payments)
search_overdue_membership_payments_button.place(x=10,y=130,width=235,height=20)

menu_window.mainloop()

Here is a screenshot of my two windows. The smaller cluttered window is a window where the user clicks on the first button in order to get the second window to appear. The larger window should have various objects on it, but instead these are all placed added to the smaller window.

2 个答案:

答案 0 :(得分:1)

您问题的字面答案是"告诉窗口小部件进入哪个窗口"。

您的代码中存在两个问题。首先是您要创建两个Tk实例。您应该始终明确地创建一个。如果您需要多个窗口,则第二个和后续窗口必须是Toplevel的实例。您还需要拨打mainloop一次。

第二个问题是您没有指定每个小部件的父级或主控。如果你不这样做,tkinter将默认为根窗口。如果您希望menu_label成为第二个"较小窗口的一部分"你只需要明确说明:

menu_window = Toplevel()
menu_label=Label(menu_window, ...)

答案 1 :(得分:0)

from tkinter import *

def new_member():

#This is for the larger window
new_member_window = Toplevel()
new_member_window.title("Add a new member")
new_member_window.geometry("500x500")

#NAME ENTRY

name_entry_label = Label(new_member_window, text = "Enter a new member's name:")
name_entry_label.place(x=7,y=-25,width=80,height=83)

name_box = Entry(new_member_window, text="")
name_box.place(x=100,y=10,width=100,height=15)

#AGE ENTRY

age_entry_label = Label(new_member_window, text = "Enter their age:")
age_entry_label.place(x=7,y=10,width=80,height=83)

age_box = Entry(new_member_window,text="")
age_box.place(x=100,y=45,width=100,height=15)

#EMAIL ADRESS ENTRY

email_entry_label = Label(new_member_window, text = "Enter their email:")
email_entry_label.place(x=10,y=45,width=80,height=83)

age_box = Entry(new_member_window, text="")
age_box.place(x=100,y=80,width=100,height=15)



#All below is for the smaller window
menu_window = Tk()
menu_window.title("Sports Club Membership")
menu_window.geometry("264x164")

menu_label=Label(text="Main menu",font=("Helvetica",20,"underline","bold"))

menu_label.place(x=10,y=10)

#ADD A NEW MEMBER BUTTON

menu_new_member_button=Button(text="Add a new member",command=new_member)
menu_new_member_button.place(x=10,y=70,width=120,height=20)

#SEARCH MEMBER JOINING DATES
   search_member_join_dates_button=Button(text="Search member joining 
 dates")#,command=member_joining_dates)
 search_member_join_dates_button.place(x=10,y=100,width=170,height=20)

   #SEARCH FOR OVERDUE MEMBERSHIP PAYMENTS

search_overdue_membership_payments_button=Button(text="Search for overdue 
membership payments")#,command=overdue_membership_payments)

search_overdue_membership_payments_button.place(x=10,y=130,width=235,height=20)




menu_window.mainloop()

请勿调用root两次,但请使用Toplevel作为您的函数,并指定要将窗口小部件放入其中的窗口。