Tkinter Program for making a calculator - python

I am trying to make a simple GUI calculator just for addition/subtraction in the beginning. I am able to print the result to the console but I want to print it to the Entry box like the First Name entry box for example but not able to do it. I would really appreciate if you could help.(*Please neglect the alignment right now of the buttons I am focusing on the functioning, trying to get it right)
from Tkinter import *
import tkMessageBox
import sys
class scanner:
list1 = []
def __init__(self,parent):
self.entrytext = StringVar()
self.entrytext1 = StringVar()
Label(root, text="first name", width=10).grid(row=0,column=0)
Entry(root, textvariable=self.entrytext, width=10).grid(row=0,column=1)
Label(root, text="last name", width=10).grid(row=1,column=0)
Entry(root, textvariable=self.entrytext1, width=10).grid(row=1,column=1)
Button(root, text="ADD", command=self.add).grid()
Button(root, text="SUBTRACT", command=self.subtract).grid()
def add(self):
global a
global b
self.a=int(self.entrytext.get())
self.b=int(self.entrytext1.get())
print "result is", self.a+self.b
def subtract(self):
global a
global b
self.a=int(self.entrytext.get())
self.b=int(self.entrytext1.get())
print "result is", self.a-self.b
root= Tk()
root.geometry("300x300")
calc = scanner(root)
root.mainloop()

If you want to show the result of the operation as a label's text, just create a new label and configure it with the text option and the string you are printing as its value. As a side note, you don't need the global statements, and the use of instance variables is not necessary either. However, it is very important to check that the content of the entries are actually valid numbers:
def __init__(self,parent):
# ...
self.result = Label(root, text='')
self.result.grid(row=4, column=0)
def add(self):
try:
a = int(self.entrytext.get())
b = int(self.entrytext1.get())
self.result.config(text=str(a+b))
except ValueError:
print("Incorrect values")

To add entry text to the widget, use the insert method. To replace the current text, you can call delete before you insert the new text.
e = Entry(master)
e.pack()
e.delete(0, END)
e.insert(0, "a default value")
The first parameter in the delete method is which number character to delete from and the second parameter is where to delete too. Notice how END is a tkinter variable.
The parameters for the insert function is where the text will be inserted too, and the second parameter is what will be inserted.
In the future, I recommend going to Effbot and reading about the widget that you are trying to use to find out all about it.

Related

How do i use tkinter python to with phrase = input("ENTER TEXT ") Tkinter text boxes?

I am trying to give my code a gui but I don't know how to use inputs with tkinter text boxes.
The code I am trying to give a gui:
phrase = input("ENTER TEXT ")
print("YOU ENTERED " + phrase + " NUMBER OF LETTERS: ")
print(len(phrase))
print("CONVERTED TO LOWER CASE:")
print(phrase.lower())
print("CONVERTED TO UPPER CASE:")
print(phrase.upper())
def say_hi():
print("BYE USER")
input('Press ENTER to exit')
say_hi()
The question is a little confusing, but this will open a message with an entry box.
import tkSimpleDialog
answer = tkSimpleDialog.askstring(title, message)
The 'title' parameter is simply the title of the message box, the text at the top.
The 'message' parameter is the message inside of the message box, such as 'Input your name'.
This is easier than other methods, as you do not need to setup a box, and position all the elements. However, if you need anything but a dialog box with an entry, you may want to try one of the other answers. :)
You can then use 'answer' however you need, exactly how you would with input()
~ Hope this helped! :D
The following code perform exactly what you do using a tkinter GUI.
Here is what you need to understand :
Label is used to create a paragraph.
Entry is a text input widget.
Entry has a get method to extract the tiped text.
To overwrite existing text in an Entry, you use it's method delete. You have to specify a range to delete.
To insert text in an Entry, you use it's method insert. You have to specify the position where the text will be inserted.
Code
import tkinter as tk
class YourGUI(tk.Tk):
def __init__(self):
# inherit tkinter's window methods
tk.Tk.__init__(self)
tk.Label(self, text="ENTER TEXT:")\
.grid(row=0, column=0)
self.input = tk.Entry(self)
self.input.grid(row=0, column=1)
tk.Label(self, text="CONVERTED TO LOWER CASE:")\
.grid(row=1, column=0)
self.result_lower = tk.Entry(self)
self.result_lower.grid(row=1, column=1)
tk.Label(self, text="CONVERTED TO UPPER CASE:")\
.grid(row=2, column=0)
self.result_upper = tk.Entry(self)
self.result_upper.grid(row=2, column=1)
tk.Button(self, text="convert", command=self.do_conversion)\
.grid(row=3, column=0, columnspan=2)
def do_conversion(self):
self.result_lower.delete(0, tk.END)
self.result_upper.delete(0, tk.END)
phrase = self.input.get()
self.result_lower.insert(0, phrase.lower())
self.result_upper.insert(0, phrase.upper())
if __name__ == '__main__':
your_gui = YourGUI()
your_gui.mainloop()
You should take a look at some tkinter tutorial like the following
https://likegeeks.com/python-gui-examples-tkinter-tutorial/
You need to go through Tkinter manual to see what kind of layout would be suitable for you. For above mentioned requirements, try this:
First import the library: import tkinter as tk
Then outside your main function/method/class: root.mainloop()
You can use tkinter grid layout. Try some example here: https://www.python-course.eu/tkinter_layout_management.php
Then:
phraseLabel = Label(window, text = "ENTER TEXT").grid(row=1,column=1) #creates label
phraseEntry = Entry(window, textvariable=phraseValue).grid(row=2,column=1) #create input text field
To retrieve the value from the text field use: phraseValue.get()
Best wishes

Updating a label from an entry field on button push with tkinter in Python 3.5.2

I am trying to create a window with a line label, an entry field, a current value label, and an "Update Value" button.
Here is an example:
This is what I have so far. I can get the entered value to print to console, but I can't seem to work out how to get an entered value and change the currentValue Label to reflect that value by pressing the button:
from tkinter import*
main=Tk()
#StringVar for currentValue in R0C2
currentValue = StringVar(main, "0")
#Called by the setValues button, looks for content in the entry box and updates the "current" label
def setValues():
content = entry.get()
print(content)
#This kills the program
def exitProgram():
exit()
#Title and window size
main.title("Title")
main.geometry("350x200")
#Descriptions on the far left
Label(main, text="Duration (min): ").grid(row=0, column=0)
#Entry boxes for values amidship
entry=Entry(main, width=10)
entry.grid(row=0, column=1)
#Displays what the value is currently set to.
currentValue = Label(textvariable=currentValue)
currentValue.grid(row=0,column=2)
#Takes any inputted values and sets them in the "Current" column using def setValues
setValues=Button(text='Set Values',width=30,command=setValues)
setValues.grid(row=9, column=0, columnspan=2)
#Red button to end program
exitButton=Button(main, text='Exit Program',fg='white',bg='red',width=30, height=1,command=exitProgram)
exitButton.grid(row=20, column = 0, columnspan=2)
main.mainloop()
There are a couple of problems with your code.
Firstly, you are overwriting the setValues function with the setValues Button widget, and similarly, you are overwriting the currentValue StringVar with the currentValue Label.
To set a StringVar, you use its .set method.
Don't use plain exit in a script, that's only meant to be used in an interactive interpreter session, the proper exit function is sys.exit. However, in a Tkinter program you can just call the .destroy method of the root window.
Here's a repaired version of your code.
import tkinter as tk
main = tk.Tk()
#StringVar for currentValue in R0C2
currentValue = tk.StringVar(main, "0")
#Called by the setValues button, looks for content in the entry box and updates the "current" label
def setValues():
content = entry.get()
print(content)
currentValue.set(content)
#This kills the program
def exitProgram():
main.destroy()
#Title and window size
main.title("Title")
main.geometry("350x200")
#Descriptions on the far left
tk.Label(main, text="Duration (min): ").grid(row=0, column=0)
#Entry boxes for values amidship
entry = tk.Entry(main, width=10)
entry.grid(row=0, column=1)
#Displays what the value is currently set to.
currentValueLabel = tk.Label(textvariable=currentValue)
currentValueLabel.grid(row=0,column=2)
#Takes any inputted values and sets them in the "Current" column using def setValues
setValuesButton = tk.Button(text='Set Values',width=30,command=setValues)
setValuesButton.grid(row=9, column=0, columnspan=2)
#Red button to end program
exitButton = tk.Button(main, text='Exit Program',fg='white',bg='red',width=30, height=1,command=exitProgram)
exitButton.grid(row=20, column = 0, columnspan=2)
main.mainloop()
BTW, it's a Good Idea to avoid "star" imports. Doing from tkinter import * dumps 130 names into your namespace, which is unnecessary and creates the possibility of name collisions, especially if you do star imports from several modules. It also makes the code less readable, since the reader has remember which names you defined and which ones came from the imported module(s).
In my opinion the easiest way to do this would be using an object orientated method. This way you could declare a button with a command that calls a def which runs self.label.configure(text=self.entry.get()).
This can be seen below:
import tkinter as tk
class App:
def __init__(self, master):
self.master = master
self.label = tk.Label(self.master)
self.entry = tk.Entry(self.master)
self.button = tk.Button(self.master, text="Ok", command=self.command)
self.label.pack()
self.entry.pack()
self.button.pack()
def command(self):
self.label.configure(text=self.entry.get())
root = tk.Tk()
app = App(root)
root.mainloop()
The above creates a label, entry and button. The button has a command which calls a def within the class App and updates the value of the label to be the text contained within the entry.
This all works very smoothly and cleanly and more importantly is drastically easier (in my opinion) to read and update in the future.
From your code you are setting the 'currentValue', which is a StringVar:
#StringVar for currentValue in R0C2
currentValue = StringVar(main, "0")
to an object Label further down in your code. You cannot do this!
#Displays what the value is currently set to.
currentValue = Label(textvariable=currentValue) ** this line is wrong
currentValue.grid(row=0,column=2)
You should name the label something different like:
#Displays what the value is currently set to.
lblCurrentValue = Label(textvariable=currentValue)
lblCurrentValue.grid(row=0,column=2)
Then in your "setValues" method you should use 'StringVar.set(value) to update the label like so:
def setValues():
content = entry.get()
currentValue.set(entry.get())------------------Here I set the value to the entry box value
print(content)
I tend to avoid stringVar and just use:
Label.config(text='*label's text*')
If you need more help I can post you my solution but try and solve it first becasue its the best way to learn. My tip is to make sure you are using correct naming conventions. In tkinter I tend to use lbl..., entryBox... etc before widgets so I know what they are and not to confuse them with variables.

Radio button values in Python Tkinter

I am trying to create a dialog box in Python using Tkinter. The goal is to have a dialog box with two radio buttons and an "OK" button. Radio button one selects the option "default". Radio button two selects the option "user defined." The "OK" button closes the window.
Question 1: How do I save the value from the radio button? That is, how do I pass the selected radio button to the rest of my script?
Question 2: How can I have the second radio button include user text input (along the lines of tkSimpleDialog.askstring)? I would like that button to show a radiobutton, a prompt ("Enter value:"), and a space for the user to enter text -- all on one line as a single radiobutton option.
So the whole dialog should have the top radio button be a normal radio button, and the second button specify user input and include a space for that user input (and the OK button).
So far I have a dialog open with two options, but the value doesn't get passed to anything I can see; selection is returned as 0 even before I select a radiobutton.
Any help on either question would be greatly appreciated, thank you.
Here's my script so far:
from Tkinter import*
master = Tk()
var = IntVar()
Label(master, text = "Select OCR language").grid(row=0, sticky=W)
Radiobutton(master, text = "default", variable = var, value = 1).grid(row=1, sticky=W)
Radiobutton(master, text = "user-defined", variable = var, value = 2).grid(row=2, sticky=W)
Button(master, text = "OK", command = master.quit).grid(row=3, sticky=W)
selection = var.get()
print "Selection:", selection
mainloop()
#If selection == 0 do one thing
#If selection == 1 do something else...
A bit late to the party, but I stumbled upon this question while trying to find something on Tkinter radiobuttons.
Question 1:
I changed three things:
1) I immediately set the value of var to 1 after you've defined it. This is done by doing var.set(1) and will make sure your first radio button is selected (which has a value of 1, as you defined it later on in the code).
2) I've replaced your master.quit command with a function called quit_loop. In this function:
The var value is printed through a print and get statement. The get will 'get' the current value of var, which depends on which radio button is selected.
I create a global variable within this function, which will then get the current value of var.
I added parentheses to master.quit() because this is no longer in the command of a radio button. Note that if you plan on using IDLE, master.destroy() might be a more suitable alternative.
3) Due to the creation of the selection variable in the function we now have your wanted value stored in a variable. There is one final if-statement at the end of the code to show it's working.
from Tkinter import *
master = Tk()
var = IntVar()
var.set(1)
def quit_loop():
print "Selection:",var.get()
global selection
selection = var.get()
master.quit()
Label(master, text = "Select OCR language").grid(row=0, sticky=W)
Radiobutton(master, text = "default", variable=var, value = 1).grid(row=1, sticky=W)
Radiobutton(master, text = "user-defined", variable=var, value = 2).grid(row=2, sticky=W)
Button(master, text = "OK", command=quit_loop).grid(row=3, sticky=W)
master.mainloop()
if selection == 1:
print "My Value is equal to one."
elif selection == 2:
print "My value is equal to two."
Question 2:
I would keep it simple and just add a label and an entry box right after your radio button. This means that we also have to work with columns as you didn't have any defined in your previous code, which makes everything default to column 0. We want your second radio button to be 'radio, label, entry' which takes three columns.
1) The previous label containing "Select OCR language" will be spanned over three columns with columnspan=3 added to the grid arguments. The same goes for your first radio button.
2) I added a Label and an Entry after your second radio button. Note that the columns go from 0 to 2, defining our three columns. The label has a simple "Enter value:" text, whereas the entry has the variable textvariable=entry_text. I added this variable entry_text to the beginning of your code and immediately set its value to ###. Note that this is a string (hence, textvariable) so adding checks for integer numbers only is up to you.
3) Of course, this is not linked to the second radio button. It still has a value of 2 if we select it, not the value of the Entry widget. That's why, in the previously created quit_loop function, I added a small if statement that assigns the value of the entry to selection if the second radio button was selected.
from Tkinter import *
master = Tk()
var = IntVar()
var.set(1)
entry_text = StringVar()
entry_text.set("###")
def quit_loop():
print "Selection:",var.get()
global selection
selection = var.get()
if selection == 2:
selection = entry_text.get()
master.quit()
# Add columnspan to these widgets
Label(master, text = "Select OCR language").grid(row=0, sticky=W, columnspan=3)
Radiobutton(master, text = "default", variable=var, value = 1).grid(row=1, sticky=W, columnspan=3)
# Order these widgets in their appropriate columns
Radiobutton(master, variable=var, value = 2).grid(row=2, sticky=W, column=0)
Label(master, text="Enter value:").grid(row=2, sticky=W, column=1)
Entry(master, textvariable=entry_text).grid(row=2, sticky=W, column=2)
# Example of what happens without columnspan
Button(master, text = "OK", command=quit_loop).grid(row=3, sticky=W)
master.mainloop()
print selection
Tip
If this simple GUI remains this small, it's ok to write code in this manner. However, expanding a lot on this further I would suggest taking an object oriented approach as it really improves readability a lot, especially when functions are being defined. That way they don't have to be necessarily defined beforehand.
Instead of directly using master.quit in the Button's command, define a function that finishes up the program then calls master.quit():
def end_program(event=None):#event will let it be used as .bind callbacks too if you need it.
selection = var.get()
if selection:
NotImplemented
else:
NotImplemented
master.quit()
...
Button(master, text = "OK", command = end_program).grid(row=3, sticky=W)
one the master is closed some of the data from the widgets is cleaned up so master.quit() needs to be called only after you are done accessing the widgets.
As set the selection value will be set before the window appears (selection = 0).
If you want to run tests after mainloop(), selection = var.get() should also be after mainloop() with tests.
If you do not want to close the master window before tests, use command=function:
from Tkinter import *
def function():
selection = var.get()
if selection == 1:
# Default
elif selection == 2:
# User-defined
else:#selection==0
#No choice
master.quit()
master = Tk()
var = IntVar()
Label(master, text = "Select OCR language").grid(row=0, sticky=W)
Radiobutton(master, text = "default", variable = var, value = 1).grid(row=1, sticky=W)
Radiobutton(master, text = "user-defined", variable = var, value = 2).grid(row=2, sticky=W)
Button(master, text = "OK", command = function).grid(row=3, sticky=W)
mainloop()

Why is Tkinter Entry's get function returning nothing?

I'm trying to use an Entry field to get manual input, and then work with that data.
All sources I've found claim I should use the get() function, but I haven't found a simple working mini example yet, and I can't get it to work.
I hope someone can tel me what I'm doing wrong. Here's a mini file:
from tkinter import *
master = Tk()
Label(master, text="Input: ").grid(row=0, sticky=W)
entry = Entry(master)
entry.grid(row=0, column=1)
content = entry.get()
print(content) # does not work
mainloop()
This gives me an Entry field I can type in, but I can't do anything with the data once it's typed in.
I suspect my code doesn't work because initially, entry is empty. But then how do I access input data once it has been typed in?
It looks like you may be confused as to when commands are run. In your example, you are calling the get method before the GUI has a chance to be displayed on the screen (which happens after you call mainloop.
Try adding a button that calls the get method. This is much easier if you write your application as a class. For example:
import tkinter as tk
class SampleApp(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.entry = tk.Entry(self)
self.button = tk.Button(self, text="Get", command=self.on_button)
self.button.pack()
self.entry.pack()
def on_button(self):
print(self.entry.get())
app = SampleApp()
app.mainloop()
Run the program, type into the entry widget, then click on the button.
You could also use a StringVar variable, even if it's not strictly necessary:
v = StringVar()
e = Entry(master, textvariable=v)
e.pack()
v.set("a default value")
s = v.get()
For more information, see this page on effbot.org.
A simple example without classes:
from tkinter import *
master = Tk()
# Create this method before you create the entry
def return_entry(en):
"""Gets and prints the content of the entry"""
content = entry.get()
print(content)
Label(master, text="Input: ").grid(row=0, sticky=W)
entry = Entry(master)
entry.grid(row=0, column=1)
# Connect the entry with the return button
entry.bind('<Return>', return_entry)
mainloop()
*
master = Tk()
entryb1 = StringVar
Label(master, text="Input: ").grid(row=0, sticky=W)
Entry(master, textvariable=entryb1).grid(row=1, column=1)
b1 = Button(master, text="continue", command=print_content)
b1.grid(row=2, column=1)
def print_content():
global entryb1
content = entryb1.get()
print(content)
master.mainloop()
What you did wrong was not put it inside a Define function then you hadn't used the .get function with the textvariable you had set.
you need to put a textvariable in it, so you can use set() and get() method :
var=StringVar()
x= Entry (root,textvariable=var)
Most of the answers I found only showed how to do it with tkinter as tk. This was a problem for me as my program was 300 lines long with tons of other labels and buttons, and I would have had to change a lot of it.
Here's a way to do it without importing tkinter as tk or using StringVars. I modified the original mini program by:
making it a class
adding a button and an extra method.
This program opens up a tkinter window with an entry box and an "Enter" button. Clicking the Enter button prints whatever is in the entry box.
from tkinter import *
class mini():
def __init__(self):
master = Tk()
Label(master, text="Input: ").grid(row=0, sticky=W)
Button(master, text='Enter', command=self.get_content).grid(row=1)
self.entry = Entry(master)
self.entry.grid(row=0, column=1)
master.mainloop()
def get_content(self):
content = self.entry.get()
print(content)
m = mini()

How to clear the Entry widget after a button is pressed in Tkinter?

I'm trying to clear the Entry widget after the user presses a button using Tkinter.
I tried using ent.delete(0, END), but I got an error saying that strings don't have the attribute delete.
Here is my code, where I'm getting error on real.delete(0, END):
secret = randrange(1,100)
print(secret)
def res(real, secret):
if secret==eval(real):
showinfo(message='that is right!')
real.delete(0, END)
def guess():
ge = Tk()
ge.title('guessing game')
Label(ge, text="what is your guess:").pack(side=TOP)
ent = Entry(ge)
ent.pack(side=TOP)
btn=Button(ge, text="Enter", command=lambda: res(ent.get(),secret))
btn.pack(side=LEFT)
ge.mainloop()
After poking around a bit through the Introduction to Tkinter, I came up with the code below, which doesn't do anything except display a text field and clear it when the "Clear text" button is pushed:
import tkinter as tk
class App(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master, height=42, width=42)
self.entry = tk.Entry(self)
self.entry.focus()
self.entry.pack()
self.clear_button = tk.Button(self, text="Clear text", command=self.clear_text)
self.clear_button.pack()
def clear_text(self):
self.entry.delete(0, 'end')
def main():
root = tk.Tk()
App(root).pack(expand=True, fill='both')
root.mainloop()
if __name__ == "__main__":
main()
I'm unclear about your question. From http://effbot.org/tkinterbook/entry.htm#patterns, it
seems you just need to do an assignment after you called the delete.
To add entry text to the widget, use the insert method. To replace the current text, you can call delete before you insert the new text.
e = Entry(master)
e.pack()
e.delete(0, END)
e.insert(0, "")
Could you post a bit more code?
real gets the value ent.get() which is just a string. It has no idea where it came from, and no way to affect the widget.
Instead of real.delete(), call .delete() on the entry widget itself:
def res(ent, real, secret):
if secret == eval(real):
showinfo(message='that is right!')
ent.delete(0, END)
def guess():
...
btn = Button(ge, text="Enter", command=lambda: res(ent, ent.get(), secret))
If in case you are using Python 3.x, you have to use
txt_entry = Entry(root)
txt_entry.pack()
txt_entry.delete(0, tkinter.END)
You shall proceed with ent.delete(0,"end") instead of using 'END', use 'end' inside quotation.
secret = randrange(1,100)
print(secret)
def res(real, secret):
if secret==eval(real):
showinfo(message='that is right!')
real.delete(0, END)
def guess():
ge = Tk()
ge.title('guessing game')
Label(ge, text="what is your guess:").pack(side=TOP)
ent = Entry(ge)
ent.pack(side=TOP)
btn=Button(ge, text="Enter", command=lambda: res(ent.get(),secret))
btn.pack(side=LEFT)
ge.mainloop()
This shall solve your problem
First of all, make sure the Text is enabled, then delete your tags, and then the content.
myText.config(state=NORMAL)
myText.tag_delete ("myTags")
myText.delete(1.0, END)
When the Text is "DISABLE", the delete does not work because the Text field is in read-only mode.
def clear():
global input
abc =
input.set(abc)
root = Tk()
input = StringVar()
ent = Entry(root,textvariable = input,font=('ariel',23,'bold'),bg='powder blue',bd=30,justify='right').grid(columnspan=4,ipady=20)
Clear = Button(root,text="Clear",command=clear).pack()
Input is set the textvariable in the entry, which is the string variable and when I set the text of the string variable as "" this clears the text in the entry
Simply define a function and set the value of your Combobox to empty/null or whatever you want. Try the following.
def Reset():
cmb.set("")
here, cmb is a variable in which you have assigned the Combobox. Now call that function in a button such as,
btn2 = ttk.Button(root, text="Reset",command=Reset)
if you add the print code to check the type of real, you will see that real is a string, not an Entry so there is no delete attribute.
def res(real, secret):
print(type(real))
if secret==eval(real):
showinfo(message='that is right!')
real.delete(0, END)
>> output: <class 'str'>
Solution:
secret = randrange(1,100)
print(secret)
def res(real, secret):
if secret==eval(real):
showinfo(message='that is right!')
ent.delete(0, END) # we call the entry an delete its content
def guess():
ge = Tk()
ge.title('guessing game')
Label(ge, text="what is your guess:").pack(side=TOP)
global ent # Globalize ent to use it in other function
ent = Entry(ge)
ent.pack(side=TOP)
btn=Button(ge, text="Enter", command=lambda: res(ent.get(),secret))
btn.pack(side=LEFT)
ge.mainloop()
It should work.
From my experience, Entry.delete(0, END) sometimes didn't work when the state of entry widget is DISABLED. Check the state of Entry when Entry.delete(0, END), doesn't work and if the value of entry widget remains, call entry.update() to reflect the result of delete(0, END).
You can Use Entry.delete(1.0, END)
it deletes every thing in the entry field, if u use only Entry.delete(1.0)it deletes the last word you inputed ->Example text to ->xample text
if none of the above is working you can use this->
idAssignedToEntryWidget.delete(first = 0, last = UpperLimitAssignedToEntryWidget)
for e.g. ->
id assigned is = en then
en.delete(first =0, last =100)
Try with this:
import os
os.system('clear')

Categories

Resources