473,486 Members | 2,353 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Accepting Tkinter input

60 New Member
I am trying to make a simple random number generator GUI, and for some reason I get an error trying to take the text out txt1 and txt2. Why isn't this working? The error says AttributeError: 'NoneType' object has no attribute 'get'

Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2. import random
  3. class myform:
  4.     def __init__(self,form):
  5.         form.wm_title('Random')
  6.         lbl1=Label(form,text='Low:').grid(row=0)
  7.         self.txt1=Entry(form).grid(row=0,column=1)
  8.         lbl2=Label(form,text='High:').grid(row=1)
  9.         self.txt2=Entry(form).grid(row=1,column=1)
  10.         btn1=Button(form,text='Submit',command=self.onClick).grid(row=2)
  11.         self.lbl3=Entry(form).grid(row=2,column=1)
  12.     def onClick(self):
  13.         low=self.txt1.get()
  14.         high=self.txt2.get()
  15.         output=random.randrange(low,high)
  16.         self.lbl3.config(text=output)
  17.         self.lbl3.update_idletasks()
  18. root=Tk()
  19. myform(root)
  20. root.mainloop()
May 10 '08 #1
2 3596
jlm699
314 Contributor
So there were two errors. The first (the one that you mentioned) was caused by the fact that you combined the initialization of Entry() with the grid command. By combining those calls into as ingle line, python took the "last" returned element and stored it to txt1 and txt2. This means instead of storing a Entry widget, it was storing the NoneType that is returned by the grid command. What you did is valid, it just doesn't give you what you want. If you wanted to still have them on a single line of code you would have to make use of the name parameter and then call the widget by name. That or make use of a text variable.

The second error was you forgot to convert the return from the get() command to int... however I'm sure you would've realized that as soon as you solved the first error. Anyway, here's the fixed code:
Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2. import random
  3.  
  4. class myform:
  5.     def __init__(self,form):
  6.         form.wm_title('Random')
  7.         lbl1=Label(form,text='Low:').grid(row=0)
  8.         self.txt1=Entry(form)
  9.     self.txt1.grid(row=0,column=1)
  10.         lbl2=Label(form,text='High:').grid(row=1)
  11.         self.txt2=Entry(form)
  12.     self.txt2.grid(row=1,column=1)
  13.         btn1=Button(form,text='Submit',command=self.onClick)
  14.     btn1.grid(row=2)
  15.         self.lbl3=Entry(form)
  16.     self.lbl3.grid(row=2,column=1)    
  17.  
  18.     def onClick(self):
  19.         low=int(self.txt1.get())
  20.     high=10
  21.         high=int(self.txt2.get())
  22.         output=random.randrange(low,high)
  23.         self.lbl3.delete(0, END)
  24.         self.lbl3.insert(0, output)
  25.  
  26. root=Tk()
  27. myform(root)
  28. root.mainloop()
May 10 '08 #2
psychofish25
60 New Member
Oh okay excellent, thanks
May 10 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
10855
by: Otto Krüse | last post by:
Hi everyone, I'm building a GUI in which I want, amongst other things, for people to fill in there postal code. The postal codes of my country (Holland) are in this format: 1234 AB So for the...
5
10535
by: Ali | last post by:
I was wondering how one would go about displaying unicode in a Label object in a Tkinter window. I am trying to display text in another language. Please help.
8
4462
by: Erik Johnson | last post by:
I am looking for some input on GUI libraries. I want to build a Python-driven GUI, but don't really understand the playing field very well. I have generally heard good things about wxPython. I...
2
3470
by: Rikkert | last post by:
I am trying to make an Edit control that accepts only numbers and one point (i.e. only accepting input of type 'double' or 'float'). Can this be done without using MFC? The standard options for...
0
1323
by: Diogo Bastos | last post by:
Hello, I'm fairly used to working with Python but it's the first time I'm trying to use Tkinter so I'm running into a problem. I'm using three python scripts with Tkinter GUIs and a fourth...
1
5085
by: vigacmoe | last post by:
Hi all, I'm trying to write a simple tkinter program, then this problem popped up. The followin code will describe the problem. ------------------------------------------ import Tkinter ...
2
3938
by: Kevin Walzer | last post by:
I'm porting a Tkinter application to wxPython and had a question about wxPython's event loop. The Tkinter app provides a GUI to a command-line tool. It gathers user input, and opens an...
0
1373
chigasakigaijin
by: chigasakigaijin | last post by:
Hi all! This is my first post in this forum. Looking forward to learning alot! I'm still a beginner at Python, but have grasped all the major concepts in "How to Think Like a Computer Scientist." ...
8
3262
by: karthikbalaguru | last post by:
Hi, One of my python program needs tkinter to be installed to run successfully. I am using Redhat 9.0 and hence tried installing by copying the tkinter-2.2.2-36.i386.rpm alone from the CD 3 to...
0
7105
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6967
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7132
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6846
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4870
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4564
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3076
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
600
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.