Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

can't able to access entry textvariable(define in function) form main code in python tkinter

Program: GUI form app in tkinter(using entry,button,tkinter)

Problem: can't get the value of entry field in main code from show() function

name.get() in submit() does not print any value in main code

Main code

if __name__ ==  '__main__':
window = Tk()
name = tk.StringVar()
def submit():
    print(name.get())
    print("inside submit")
Alpha(window)
window.title('Alpha Inventory System...!')
window.geometry('930x565')
window.mainloop()

Alpha init, here I call show() method(at 'item3' line) which further call submit method

def __init__(self,win):
   
    chooser = Menu()
    item1 = Menu(tearoff=0)
    item1.add_command(label='User Account details')
    item1.add_command(label='User persaonal details')
    
    item2 = Menu(tearoff=0)
    item2.add_command(label='Admin Account Details')
    item2.add_command(label='Admin Personal details')
    
    item3 = Menu(tearoff=0)
    item3.add_command(label='Add Customer',command=self.show)
    item3.add_command(label='Delete Customer',command=self.delete_cust)
    
    item4 = Menu(tearoff=0)
    item4.add_command(label='Add Product',command=self.addproduct)
    item4.add_command(label='Delete Product',command=self.delete)
    item4.add_command(label='Update Product',command=self.update)
    item4.add_command(label='View/Search Product')
    
    item5 = Menu(tearoff=0)
    item5.add_command(label='Generate order')

    
    item6 = Menu(tearoff=0)
    
    item7 = Menu(tearoff=0)
    item7.add_command(label='Exit',command=self.ExitApplication)
    
    
    chooser.add_cascade(label='User Profile',menu=item1)
    chooser.add_cascade(label='Admin Master',menu=item2)
    chooser.add_cascade(label='Customer Master',menu=item3)
    chooser.add_cascade(label='Product Master',menu=item4)
    chooser.add_cascade(label='Order Master',menu=item5)
    chooser.add_cascade(label='Report',menu=item6)
    chooser.add_cascade(label='Exit',menu=item7)
    
    win.config(menu=chooser)

show() method, here I set textvariable=name in "tk.Entry" method. It can't access form 'main_code' where I print it, name.get() is empty there

  text = tk.Entry(f1, textvariable=name,fg='blue', bg='lightyellow')

Full portion code

def show(self):
    global name
    window = Tk()
    window.title('Add Customer')
    window.geometry('700x600')
    frame = Frame(window, width=100, highlightbackground='green', highlightthickness='1', bg="white")
    frame.place(x=20, y=20, height=510, width=550)
    f4 = Frame(frame, width=100, highlightbackground='black', highlightthickness='1', bg='lightgray')
    f4.place(x=20, y=430, height=50, width=500)
    f3 = Frame(frame, width=100, highlightbackground='white', highlightthickness='1', bg='lightgray')
    f3.place(x=20, y=60, height=350, width=500)
    f1 = Frame(f3, width=100, highlightbackground='black', highlightthickness='1')
    f1.place(x=20, y=30, height=310, width=450)
    f2 = Frame(frame, width=100, highlightbackground='black', highlightthickness='1', bg='lightgray')
    f2.place(x=50, y=10, height=40, width=380)
    l1 = Label(f2, text="Customer Account Details", font=('Arial', 17), bg="lightgray")
    l1.place(x=70, y=5)
    l2 = Label(f3, text="Enter user personal detail here.......", bg="lightgray")
    l2.place(x=20, y=5)
    namevar = Label(f1, text='Name', font=('Arial', 12))
    namevar.place(x=40, y=60)

    text = tk.Entry(f1, textvariable=name,fg='blue', bg='lightyellow')
    text.place(x=200, y=60)
    Address = Label(f1, text='Address', font=('Arial', 12))
    Address.place(x=40, y=100)

    Address = tk.Entry(f1, fg='blue', bg='lightyellow')
    Address.place(x=200, y=100, height=80, width=200)
    phoneno = Label(f1, text='Phone No', font=('Arial', 12))
    phoneno.place(x=40, y=200)

    phoneno = Entry(f1, fg='blue', bg='lightyellow')
    phoneno.place(x=200, y=200)
    email = Label(f1, text='Email id', font=('Arial', 12))
    email.place(x=40, y=250)

    email = Entry(f1, fg='blue', bg='lightyellow')
    email.place(x=200, y=250)
    button = Button(f4, text='SAVE', fg='black', width=15,command=submit)
    button.place(x=180, y=10)

    window.mainloop()
question from:https://stackoverflow.com/questions/66045362/cant-able-to-access-entry-textvariabledefine-in-function-form-main-code-in-py

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...