简体   繁体   English

TkInter:如何使对象出现在第二个而不是第一个窗口上?

[英]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 . 首先是要创建两个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 . 如果需要多个窗口,则第二个窗口和后续窗口必须是Toplevel实例。 You also need to call mainloop exactly once. 您还需要只调用一次mainloop

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. 如果不这样做,则tkinter将默认为根窗口。 If you want menu_label to be part of the second "smaller window" you simply need to state that explicitly: 如果希望menu_label成为第二个“较小窗口”的一部分,则只需明确声明:

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. 不要两次调用root,而要对函数使用Toplevel,并指定要在其中放置小部件的窗口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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