[Tkinter] Input box - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: [Tkinter] Input box (/thread-23422.html) |
Input box - Jpen10 - Dec-29-2019 I'm not very good at programming with tkinter and kivy. To set on the old Basic, Q basic, GWBasic and VBasic for Excel and MSAccess. I've tried numerous times to make a input box for the 11 digit UPC code. I now enter a "string" before the program runs on line 9. And the "while" works when I run the code without tkinter. When I add a input box I get a "nonsubscrptible error at the" For" statements. So I gave up. I've used this same code (with modifications) for quite a few years with Access and Excel. Not important, but would like to know how to solve the problems that arise. When the code runs I do get the correct output. Received some excellent help on this forum for the "join" statements. Thank you for any help. # Computes last (12th) number of an 11 digit UPC """completeUPC.py""" import sys import os from tkinter import * import tkinter as tk root = tk.Tk() UPCNUM='19132910212' odd=0 even=0 #while UPCNUM not in range(0,9) and len(UPCNUM) != 11: #Add odd for i in range(0,11,2): odd1=int(UPCNUM[i]) odd=odd+odd1 # multiply odd by 3 odd=odd*3 #Add even for i in range(1,11,2): even2=int(UPCNUM[i]) even=even+even2 #Determine last number lastnum=odd+even lastnum=lastnum%10 lastnum=10 - lastnum if lastnum == 10: lastnum = 0 #12 digit UPC finalnumber=UPCNUM + str(lastnum) #configure font code s1 = finalnumber s2 = 'PQRSTUVWXY' s3 = '0123456789' s4 = '@ABCDEFGHI' s5='`abcdefghi' first_str="" last_str="" pre_str="" final_str = "" divdr=chr(112) pre_str =pre_str.join(s2[int(ch)] for ch in s1[0]) first_str=first_str.join(s3[int(ch)] for ch in s1[1:6]) last_str=last_str.join(s4[int(ch)] for ch in s1[6:11]) final_str = final_str.join(s5[int(ch)] for ch in s1[11]) upc_font=pre_str+first_str+divdr+last_str+final_str #Copies to clipboard r = Tk() r.withdraw() r.clipboard_clear() r.clipboard_append(upc_font) r.update() # now it stays on the clipboard after the window is closed r.destroy() #Produces label tk.Label(root, text=upc_font, relief=RAISED, borderwidth=10, fg = "red", bg = "white", font = "UPCARev 16").pack() #Quit button Button(root, text="Quit", fg="black", bg="green", font="helvetica 16", command=root.destroy).pack() root.mainloop() RE: Input box - woooee - Dec-29-2019 #while UPCNUM not in range(0,9)UPCNUM is never going to be in range(0. 9) because it is an 11 digit number being compared to a one digit number. You might want to use isdigit(). RE: Input box - Jpen10 - Dec-29-2019 The range 0 to 9 is to limit input to numbers and 11 digits. Since it is a string I have it as a comment. If I use =input () I get AN EOF Error. If I trap for the error I get the nontsubscritable error. RE: Input box - woooee - Dec-30-2019 How do you expect help? Quote:I now enter a "string" before the program runs on line 9You do not get input anywhere in this code. Quote:And the "while" works when I run the code without tkinterThere is no wile statement in the code you posted. Quote:When I add a input box I get a "nonsubscrptible error at the" For" statementShow the code. You are obviously doing it wrong. Quote:I've used this same code (with modifications) for quite a few years with Access and Excel.Don't care. They are completely different programming languages. RE: Input box - Jpen10 - Dec-30-2019 I realize there is no input. That is the problem. The "while" on line 12 is commented out so the code runs. As of now, I enter the string on line 9. I would like a imput box to enter the 11 digits. Then run the code that follows, starting with the "for" statement on line 14 through line 50 and give the output of line 62. RE: Input box - joe_momma - Dec-31-2019 try if: if UPCNUM not in range(0,9) and len(UPCNUM) != 11: print('it failed to comply with my standards')let the mainloop be the while statement- meaning it will always be waiting for a new input or user interaction. You have initialized Tk twice you can remove the second- root.clipboard_clear() root.clipboard_append(upc_font) root.update()Your terminology is confusing in the title you have input box but just a label and a button, if you want good answers work on the questions sorry I final figured out what you're asking guizero is great for people starting... you can find the info here: documentation an example here from guizero import App, TextBox, Text, PushButton def calculate_upc(): """Insert your code here""" text= 'your entry {}'.format(input_box.value) print(text) app = App(layout='auto') label= Text(app, text='Enter UPC', align='left') input_box = TextBox(app,align='right',width='fill') btn= PushButton(app, text='Submit', command=calculate_upc, align='bottom') app.display() RE: Input box - Jpen10 - Jan-01-2020 Still can't get the 11 digits to run in the code. If I add a string of 11 numbers in line 23;"UPCNUM='96325932563' it works fine. And the 11 digits show If I enter 11 in the input box and press the "UPC number. "When I run the code with "UPCNUM=get11_digit" it gives me a "not iterable line 26." And if I code line 23 as "UPCNUM=get11_digit" it says get11_digit is not defined. # Last (12th) number of an 11 digit UPC """comp_final_1_12020.py""" import tkinter as tk root= tk.Tk() def get11_digit (): x1 = entry1.get() label1 = tk.Label(root, text= x1) canvas1.create_window(200, 230, window=label1) canvas1 = tk.Canvas(root, width = 400, height = 400) canvas1.pack() entry1 = tk.Entry (root) canvas1.create_window(200, 140, window=entry1) button1 = tk.Button(text='UPC Number', command=get11_digit) canvas1.create_window(200, 180, window=button1) odd=0 even=0 n=2 UPCNUM=get11_digit #Add odd for i,x in enumerate(UPCNUM): if i % n == 0: odd=odd+int(x) odd=odd*3 #Add even for i,x in enumerate(UPCNUM): if i % n == 1: even=even+int(x) #Determine last number lastnum=odd+even lastnum=lastnum%10 lastnum=10 - lastnum if lastnum == 10: lastnum = 0 #12 digit UPC UPC_12=UPCNUM + str(lastnum) #configure font code s1 = UPC_12 s2 = 'PQRSTUVWXY' s3 = '0123456789' s4 = '@ABCDEFGHI' s5='`abcdefghi' first_str="" last_str="" pre_str="" final_str = "" divdr=chr(112) pre_str =pre_str.join(s2[int(ch)] for ch in s1[0]) first_str=first_str.join(s3[int(ch)] for ch in s1[1:6]) last_str=last_str.join(s4[int(ch)] for ch in s1[6:11]) final_str = final_str.join(s5[int(ch)] for ch in s1[11]) upc_font=pre_str+first_str+divdr+last_str+final_str label2 = tk.Label(root,text=upc_font) canvas1.create_window(200,300, window=label2) button2 = tk.Button(text='Quit', command=root.quit) canvas1.create_window(200,350,window=button2) root.mainloop() RE: Input box - Jpen10 - Jan-27-2020 Finally! After several days of frustrarion and trials, I finally got it to work. Not real proud of the code, however, it works. |