简体   繁体   中英

TkInter: how can I make objects appear on my second window rather than the first?

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.

The literal answer to your question is "tell the widget which window to go in".

There are two problems in your code. The first is that you are creating two instances of Tk . You should always explicitly create exactly one. If you need more than one window, the second and subsequent windows need to be instances of Toplevel . You also need to call mainloop exactly once.

The second problem is that you aren't specifying the parent or master of each widget. If you don't, tkinter will default to the root window. If you want menu_label to be part of the second "smaller window" you simply need to state that explicitly:

menu_window = Toplevel()
menu_label=Label(menu_window, ...)
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()

Don't call root twice but use Toplevel for your function and also specify the window you want to place your widget inside.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM