如何获取在 tkinter 的顶级小部件中输入的值并在 main window 中使用它来显示?
How to get a value entered in Top level widget in tkinter and use it in main window to display?
我是 Python GUI 的新手并尝试学习使用 tkinter,我创建了一个主 window 单击主 window 中的“继续”按钮,一个顶层 window 打开,可以在输入框中输入一个数字,我无法读取主 window 中的值。任何人都可以帮助我如何读取在顶层 window 中输入的值并在主 window
中输入相同的值
from tkinter import *
# global variable
blank_2 = []
blank_1 = []
# Function to create a top level window with entry boxes
def open_new_window():
# Toplevel object which will be treated as a new window
popup = Toplevel(main)
global blank_2
global blank_1
# sets the title of the Toplevel widget
popup.title("Enter the value")
# sets the geometry of toplevel
popup.geometry("480x220")
popup.geometry("+500+250")
popup.configure(background='grey')
PL = Label(popup, text="Enter the 1st value :", fg="white", bg="grey",
font=('century gothic', 12))
PL.place(x=20, y=20)
GW = Label(popup, text="Enter the 2nd value :", fg="white", bg="grey",
font=('century gothic', 12))
GW.place(x=20, y=70)
blank_2 = Entry(popup, justify=LEFT, font=12, fg="black", width=15)
blank_2.place(x=210, y=20)
blank_1 = Entry(popup, justify=LEFT, font=12, fg="black", width=15)
blank_1.place(x=210, y=70)
exitp = Button(popup, text='Quit', font=8, width=5, height=1, bd=4, command=popup.destroy)
setp = Button(popup, text='Set', font=8, bd=4, command=sum)
exitp.place(x=300, y=120)
setp.place(x=240, y=120)
blank_2.focus()
# Function to insert the value from the top level window to main window
def sum():
global blank_2, blank_1
LS.insert(END, blank_2)
LS.insert(END, blank_1)
main = Tk()
main.title("TEST") # required text on window title
main.configure(background='grey') # Background color of Main window
main.geometry("+600+150") # Position of the window on the screen
main.geometry("400x450") # Size of the window
# text window to display the value in main window
LS = Text(main, bd=3, height=3, width=30, bg="light cyan")
LS.place(x=20, y=190)
# buttons to open a top level window and to quit the main window
C = Button(main, text='Continue', font=8, bd=4, command=open_new_window)
C.place(x=40, y=45)
Q = Button(main, text='Quit', font=8, width=5, height=1, bd=4, command=main.destroy)
Q.place(x=150, y=45)
mainloop()
The output in the main window
您需要调用 Entry 类的 get
方法
LS.insert(END, blank_2.get())
LS.insert(END, blank_1.get())
因为您将 blank_1 和 blank_2 定义为条目:
blank_2 = Entry(popup, justify=LEFT, font=12, fg="black", width=15)
blank_1 = Entry(popup, justify=LEFT, font=12, fg="black", width=15)
.!toplevel.!entry
是 tkinter 对 wdiget 的引用。
我是 Python GUI 的新手并尝试学习使用 tkinter,我创建了一个主 window 单击主 window 中的“继续”按钮,一个顶层 window 打开,可以在输入框中输入一个数字,我无法读取主 window 中的值。任何人都可以帮助我如何读取在顶层 window 中输入的值并在主 window
中输入相同的值from tkinter import *
# global variable
blank_2 = []
blank_1 = []
# Function to create a top level window with entry boxes
def open_new_window():
# Toplevel object which will be treated as a new window
popup = Toplevel(main)
global blank_2
global blank_1
# sets the title of the Toplevel widget
popup.title("Enter the value")
# sets the geometry of toplevel
popup.geometry("480x220")
popup.geometry("+500+250")
popup.configure(background='grey')
PL = Label(popup, text="Enter the 1st value :", fg="white", bg="grey",
font=('century gothic', 12))
PL.place(x=20, y=20)
GW = Label(popup, text="Enter the 2nd value :", fg="white", bg="grey",
font=('century gothic', 12))
GW.place(x=20, y=70)
blank_2 = Entry(popup, justify=LEFT, font=12, fg="black", width=15)
blank_2.place(x=210, y=20)
blank_1 = Entry(popup, justify=LEFT, font=12, fg="black", width=15)
blank_1.place(x=210, y=70)
exitp = Button(popup, text='Quit', font=8, width=5, height=1, bd=4, command=popup.destroy)
setp = Button(popup, text='Set', font=8, bd=4, command=sum)
exitp.place(x=300, y=120)
setp.place(x=240, y=120)
blank_2.focus()
# Function to insert the value from the top level window to main window
def sum():
global blank_2, blank_1
LS.insert(END, blank_2)
LS.insert(END, blank_1)
main = Tk()
main.title("TEST") # required text on window title
main.configure(background='grey') # Background color of Main window
main.geometry("+600+150") # Position of the window on the screen
main.geometry("400x450") # Size of the window
# text window to display the value in main window
LS = Text(main, bd=3, height=3, width=30, bg="light cyan")
LS.place(x=20, y=190)
# buttons to open a top level window and to quit the main window
C = Button(main, text='Continue', font=8, bd=4, command=open_new_window)
C.place(x=40, y=45)
Q = Button(main, text='Quit', font=8, width=5, height=1, bd=4, command=main.destroy)
Q.place(x=150, y=45)
mainloop()
The output in the main window
您需要调用 Entry 类的 get
方法
LS.insert(END, blank_2.get())
LS.insert(END, blank_1.get())
因为您将 blank_1 和 blank_2 定义为条目:
blank_2 = Entry(popup, justify=LEFT, font=12, fg="black", width=15)
blank_1 = Entry(popup, justify=LEFT, font=12, fg="black", width=15)
.!toplevel.!entry
是 tkinter 对 wdiget 的引用。