我对Tkinter的GUI界面有疑问。文本不会在外壳中打印,但是如果Entry放置在第一个tk窗口get()中,它将起作用...帮助我吗?

这是我的代码:

import os
from tkinter import *
##CONFIG FRS TK
import tkinter as tk
window = Tk()
window.geometry("600x500")
window.title("Gestione caldaie")
window.config(bg="gray")

name2=StringVar()
def enter():
    print(name2.get())

def test():
    clienti_caldaie=Tk()
    clienti_caldaie.resizable(False,False)
    clienti_caldaie.geometry("800x550")
    clienti_caldaie.title("Clienti e Caldaie")
    clienti_caldaie.config(bg = "#9999ff")

    ##DATI UTENTE
    name = Label(clienti_caldaie, text = "Nome e Cognome ", font = "bold", bg = "#9999ff").place(x = 0, y = 3)

    named = Entry(clienti_caldaie,textvariable=name2).place(x = 150, y = 4)
    but=Button(text="Save",command=enter).place(x = 70, y = 10)

clienti = Button(text = "New", command=test).place(x = 30, y = 60)
window.mainloop()

最佳答案

正如我在评论中提到的,您不应使用多个Tk()实例。您应该将Toplevel用于新对话框。

例如

import os
from tkinter import *
##CONFIG FRS TK
import tkinter as tk
window = Tk()
window.geometry("600x500")
window.title("Gestione caldaie")
window.config(bg="gray")

name2=StringVar()
def enter():
    print(name2.get())

def test():
    clienti_caldaie=Toplevel()
    clienti_caldaie.resizable(False,False)
    clienti_caldaie.geometry("800x550")
    clienti_caldaie.title("Clienti e Caldaie")
    clienti_caldaie.config(bg = "#9999ff")

    ##DATI UTENTE
    name = Label(clienti_caldaie, text = "Nome e Cognome ", font = "bold", bg = "#9999ff").place(x = 0, y = 3)

    named = Entry(clienti_caldaie,textvariable=name2).place(x = 150, y = 4)
    but=Button(clienti_caldaie,text="Save",command=enter).place(x = 70, y = 10)

clienti = Button(text = "New", command=test).place(x = 30, y = 60)
window.mainloop()

10-06 10:38