Tkinter新窗口

时间:2018-08-31 13:17:01

标签: python tkinter

我是Tkinter的新手,我需要帮助。

当从父窗口中单击一个按钮时,我创建了一个新窗口。新窗口是def new_Window。但是我似乎无法在窗口中获得如下所示的信息:

from tkinter import *
from tkinter import ttk



#User Interface Code

root = Tk() #Creates the window
root.title("Quiz Game")

def new_window(): 
    newWindow = Toplevel(root)
   display = Label(newWindow, width=200, height=50)
   message = Label(root, text="Welcome")
   display.pack()
   message.pack()


display2 = Label(root, width=100, height=30)

button1 = Button(root, text ="Continue", command=new_window, width=16, 
bg="red")

message_label = Label(root, text="Click 'Continue' to begin.", 
wraplength=250)

username = StringVar() #Stores the username in text
user_entry = Entry(root, textvariable=username) #Creates an entry for the 
username 
user_entry.pack()
display2.pack()
button1.pack()
message_label.pack()

root.mainloop()#Runs the main window loop

感谢您的帮助。

1 个答案:

答案 0 :(得分:3)

您没有将hello标签打包到新窗口中。还有一个技巧是在显影时使用背景色来可视化标签。这是适合您的功能代码。我只更改了2行,添加了前景和背景。

from tkinter import *
from tkinter import ttk



# User Interface Code

root = Tk() # Creates the window
root.title("Quiz Game")

def new_window(): 
   newWindow = Toplevel(root)
   display = Label(newWindow, width=200, height=50,bg='RED')
   message = Label(newWindow, text="HEEEY",fg='BLACK',bg='GREEN')
   message.pack()
   display.pack()


display2 = Label(root, width=100, height=30)

button1 = Button(root, text ="Continue", command=new_window, width=16, 
bg="red")

message_label = Label(root, text="Click 'Continue' to begin.", 
wraplength=250)

username = StringVar() # Stores the username in text
user_entry = Entry(root, textvariable=username) # Creates an entry for the 
username 
user_entry.pack()
display2.pack()
button1.pack()
message_label.pack()

root.mainloop() # Runs the main window loop