-4

在 tkinter 中,如何.get()从顶级窗口输入?

def logika(event):
    a=e.get()

def pocetak(event):
    igra=Toplevel(glavni)
    igra.geometry("500x500+710+290")

    e=Entry(igra)
    e.pack()

    GumbIgra=Button(igra,text="Unos")
    GumbIgra.bind("<Button>",func=logika)
    GumbIgra.pack()
    return

glavni=Tk()
glavni.geometry("600x600")
glavni.resizable(True,True)

glavniGumb=Button(glavni,text="Za početak stisni me!",pady=10,padx=15)
glavniGumb.config(font=("Arial",10))
glavniGumb.bind("<Button>",func=pocetak)
glavniGumb.pack()
4

1 回答 1

1

似乎该logika(event)功能无法弄清楚是什么e。您将不得不传递对象。

所以像这样(未经测试):

from tkinter import *
#Creating main window
root = Tk()

def Input_Box():
    # creating a top window
    master_2 = Toplevel(root)

    #Textboxes
    user_name = Entry(master_2)
    user_name.grid(row = 1, column = 2)
    pwd = Entry(master_2)
    pwd.grid(row = 2, column = 2)

    label_un = ttk.Label(master_2, text = "Username")
    label_un.grid(row = 1, column = 1)
    label_pwd = ttk.Label(master_2, text = "Password")
    label_pwd.grid(row = 2, column = 1)

    get_button = Button(master_2, text = "Confirm", command = lambda: getname(user_name))
    get_button.grid(row=3, column = 1)
    master_2.mainloop() 

def getname(user_name):
    input = user_name.get()
    print(input)


call_button = Button(root, text='Enter Usrnm and pwd', command = Input_Box)
call_button.pack()
root.mainloop()

command = lambda: getname(user_name)传递user_name引用文本框的对象。希望这可以帮助!

请告诉我们确切的错误。

PS:这是为了别的东西,但我认为这应该会有所帮助。

于 2017-02-23T19:45:58.693 回答