Я новичок в графическом интерфейсе Python и пытаюсь научиться использовать tkinter, я создал главное окно при нажатии кнопки «Продолжить» в главном окне, открывается окно верхнего уровня, и в поле ввода можно ввести число, я m не удалось прочитать значение в главном окне. Может ли кто-нибудь помочь мне с тем, как прочитать значение, введенное в окне верхнего уровня, и ввести то же самое в главном окне
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()
Вам нужно вызвать метод get
Entry, например
LS.insert(END, blank_2.get())
LS.insert(END, blank_1.get())
так как вы определили пустые_1 и пустые_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)
.!верхний уровень.!вход
является ссылкой для tkinter на файл wdiget.