python - Getting a entered variable to print - somethings amiss -


i'm bit stuck getting 'get' function work correctly. error occurs when click search button, brings 'entry1' not defined message in shell. i've tried positioning variable declaration in different positions, commenting out , variety of other tests have come stumps i'm doing wrong, i'm sure obvious beginner need pointer.

the code should print numbers entered shop order field python shell , work in progress, hence much.

from tkinter import * tkinter import ttk #import sys  class gui:     def __init__(self, rootwindow):          #variable         entry1 = stringvar()                 var2 = 3         var3 = 4           #item1         self.label = ttk.label(rootwindow, text="shop order:")         self.label.grid(row=1, column=1, sticky=(e), padx=3, pady=3)          self.entry1 = ttk.entry(rootwindow, width=5)         self.entry1.grid(row=1, column=2, sticky=(w, e), padx=3, pady=3)          #item2         self.label2 = ttk.label(rootwindow, text="quantity:")         self.label2.grid(row=1, column=3, sticky=(e), padx=3, pady=3)          self.entry2 = ttk.entry(rootwindow, width=5, textvariable=var2)         self.entry2.grid(row=1, column=4, sticky=(w, e), padx=3, pady=3)           #item3         self.label3 = ttk.label(rootwindow, text="description:")         self.label3.grid(row=2, column=1, sticky=(e), padx=3, pady=3)          self.entry3 = ttk.entry(rootwindow, width=5, textvariable=var3)         self.entry3.grid(row=2, column=2, columnspan=3, sticky=(w, e), padx=3, pady=3)           #output         self.label4 = ttk.label(rootwindow, text="drawings:")         self.label4.grid(row=3, column=1, sticky=(n,e), padx=3, pady=3)          #buttons          self.button3 = ttk.button(rootwindow, text="exit", command=rootwindow.destroy)         self.button3.grid(row=3, column=6, sticky=(n,w), padx=3, pady=3)          self.text1 = text(rootwindow, width=40, height=15, background="white")         self.text1.grid(row=3, column=2, columnspan=4, padx=3, pady=3)          def doasearch():             print (entry1.get())          self.button1 = ttk.button(rootwindow, text="search", command=doasearch)         self.button1.grid(row=1, column=6, sticky=w, padx=3, pady=3)          def clear_text():             entry1.delete(0, 'end')          self.button2 = ttk.button(rootwindow, text="reset", command=clear_text)         self.button2.grid(row=2, column=6, sticky=w, padx=3, pady=3)          def onexit(self):             self.quit()  def main():     global label        rootwindow = tk()     rootwindow.title("some random title")     rootwindow.geometry("500x325+600+300")     gui = gui(rootwindow)     rootwindow.mainloop()  main() 

i ran code , did not entry1 not defined error, able recreate issue doasearch function not printing shell entered in entry widget shop order. have been able remedy first renaming stringvar() variable entry10 instead of entry1 since program confusing entry widget stringvar() both assigned same variable. also, in first entry widget, did not assign text variable method it, not outputting entered shop order: field anyways. finally, in clear_text function, exception name 'entry1' not defined thrown since entry1 attribute of self, , entry1 cannot return entered assigned entry widget. instead self.entry1 should used that. therefore, following changes code should make work expected:

  • replace

    entry1 = stringvar() 

    with

    entry10 = stringvar() 
  • add textvariable entry widget, so:

    self.entry1 = ttk.entry(rootwindow, width=5, textvariable = entry10) 
  • in doasearch function, replace

    print (entry1.get()) 

    with

    try:     print(int(entry10.get())) except valueerror:     messagebox.showwarning("not number", "that not number. please enter number") 

    to make sure prints out number if user enters 1 in shop order field, otherwise show warning dialog if user inputs other number.

  • in clear_text function, replace

    entry1.delete(0, 'end') 

    with

    self.entry1.delete(0, 'end') 

following above changes, when user enters, instance, 50 in shop order entry widget, output in shell 50. otherwise, if other number entered, following warning dialog shown:

warning dialog

also, when user selects reset button now, shop order entry box cleared.


Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -