Calculator using Tkinter - python

I was trying to make a calculator using Tkinter in python 3.8 and I managed to do the Buttons and the label and to make Addition using 2 numbers
the problem is I want to make it continue adding if I add more numbers But I couldn't do it.
here is a simple version of my code.
code :
import tkinter as tk
first = None
method = None
second = None
result = None
def OnButtonClick(button_id):
global first
global second
global method
global result
if first == None :
first = button_id
elif first != None and button_id == "+":
method = button_id
elif first != None and second == None and method != None:
second = button_id
elif first != None and second != None and button_id == "=":
result = first + second
print(result)
window = tk.Tk()
window.title('Calculator')
window.geometry("307x500")
button = window.title('Calculator')
window.geometry("307x500")
B1 = tk.Button(window, text = "1", width = 2, height = 2, command=lambda: OnButtonClick(1) ).grid(row = 7, column = 0)
B2 = tk.Button(window, text = "2", width = 2, height = 2, command=lambda: OnButtonClick(2)).grid(row = 7, column = 1)
B3 = tk.Button(window, text = "3", width = 2, height = 2, command=lambda: OnButtonClick(3)).grid(row = 7, column = 2)
BPlus = tk.Button(window, text = "+", width = 2, height = 2, command=lambda: OnButtonClick("+")).grid(row = 7, column = 3)
BEqual = tk.Button(window, text = "=", width = 2, height = 2, command=lambda: OnButtonClick("=")).grid(row = 8, column = 3)
window.mainloop()

The way you are doing it is really messy, no offence. In this way, you would need to use a lot of base cases, which might take a long time to code.
A simpler way to do this would be to use the eval(). If the button_id is the equal button, then we can just use eval() to calculate the calculation because we are appending the other button_id's to the string op.
Here is the code:
import tkinter as tk
first = None
method = None
second = None
result = None
op = ""
def OnButtonClick(button_id):
global first, second, method, result, op
if button_id == "=":
print(eval(op))
else:
op += str(button_id)
window = tk.Tk()
window.title('Calculator')
window.geometry("307x500")
button = window.title('Calculator')
window.geometry("307x500")
B1 = tk.Button(window, text = "1", width = 2, height = 2, command=lambda: OnButtonClick(1) ).grid(row = 7, column = 0)
B2 = tk.Button(window, text = "2", width = 2, height = 2, command=lambda: OnButtonClick(2)).grid(row = 7, column = 1)
B3 = tk.Button(window, text = "3", width = 2, height = 2, command=lambda: OnButtonClick(3)).grid(row = 7, column = 2)
BPlus = tk.Button(window, text = "+", width = 2, height = 2, command=lambda: OnButtonClick("+")).grid(row = 7, column = 3)
BEqual = tk.Button(window, text = "=", width = 2, height = 2, command=lambda: OnButtonClick("=")).grid(row = 8, column = 3)
window.mainloop()
Hope this helps!

Related

Description photo viewer in GUI - Python

I'm new in Python, I am searching for solution with this error. I got stuck with this assignment.
I'm trying to change the description by each picture but unfortunately I failed.
from tkinter import *
from PIL import Image, ImageTk
root = Tk()
root.title("Image Viewer")
root.config(bg = "Grey")
frame1 = Frame(root, width = 500, height = 325, bg = "Silver")
frame1.pack(side = TOP)
frame2 = Frame(root, width = 500, height = 25, borderwidth= 1, bg = "Grey")
frame2.pack(side = BOTTOM, pady= 2)
# Image:
img1 = ImageTk.PhotoImage(Image.open("dec19.jpg"))
img2 = ImageTk.PhotoImage(Image.open("dec20.jpg"))
img3 = ImageTk.PhotoImage(Image.open("dec21.jpg"))
# Description:
des1= Label(frame1, text = "I am happy this day")
des2= Label(frame1, text = "going somewhere")
des3= Label(frame1, text = "Today is a great day")
# , width = 500, height = 315
num = 1
# List:
img_list = [img1, img2,img3]
des_list = [1 , 2, 3]
# Startup:
my_label = Label(frame1,image = img1)
my_label.pack()
my_des = Label(frame1, text = f"Description{num}")
my_des.pack(side = BOTTOM)
# Definning Command functions:
def close_app():
exit()
def forward(image_num):
global my_label
global prev
global next1
global num
global my_des
global des_list
my_label.pack_forget()
# .grid_forget()
my_label = Label(frame1, image= img_list[image_num-1])
my_label.pack()
my_des.pack_forget()
my_des = Label(frame1, text = f"Description{des_list[image_num-1]}")
my_des.pack()
# .grid(row = 0, column = 0, columnspan = 3)
next1 = Button(frame2, text = "Next", command = lambda: forward(image_num+1))
next1.grid(row = 1, column = 2)
prev = Button(frame2, text = "Previous", command = lambda : back(image_num-1))
prev.grid(row = 1, column = 0)
if image_num == 3:
next1 = Button(frame2, text = "Next", state = DISABLED)
next1.grid(row = 1, column = 2)
prev = Button(frame2, text = "Previous", state = DISABLED, command =lambda : back(2))
prev.grid(row = 1, column = 0)
exits = Button(frame2, text = "Exit", command = close_app)
exits.grid(row = 1, column = 1)
next1 = Button(frame2, text = "Next", command = lambda: forward(2))
next1.grid(row = 1, column = 2)
root.mainloop()
If you want that each description is different you can create a dictionary wher you bind each image index to its description
image_description = {
1 : 'I am happy this day',
2 : "going somewhere",
3: "Today is a great day"
}
Now that you have this dictionary when you call the function that changes the image instead having a static string that changes only the number ( f"Description{des_list[image_num-1]}" ) you can get the datas from the dictionary
so change this 3 lines
my_des.pack_forget()
my_des = Label(frame1, text = f"Description{des_list[image_num-1]}")
my_des.pack()
to this
my_des.configure(text = f"{image_description[image_num]}")

Tkinter Fibonacci Program Only Displays 1st Number (python 3)Ki

I'm writing a program that shows the Fibonacci sequence one number at a time. You are supposed to put in the number of numbers you want, and the numbers show one at a time. However, when I press the button, it just shows 1.
The first version of the code didn't work, because you would press the button and just get the final number. I tried (and failed) to fix it by adding "return fib" to the function, but that caused the current problem.
from time import sleep
from tkinter import *
def calc():
a = 1
b = 0
c = 0
w = texxt.get()
num = int(w)
for i in range(num):
if a >= b:
fib.set(str(a))
elif b >= a:
fib.set(str(b))
else:
fib.set("ERROR")
break
c = a + b
if a >= b:
b = c
elif b >= a:
a = c
else:
pass
return fib
sleep(1.2)
root = Tk()
fib = StringVar()
texxt = StringVar()
root.title("Fibonacci Calculator")
entry = Entry(root, textvariable = texxt)
entry.grid(row = 0, column = 0, sticky = 'nsew', padx = 3, pady = 3)
button = Button(root, text = "Start", command = calc)
button.grid(row = 1, column = 0, sticky='nsew', padx = 3, pady = 3)
label = Label(root, width = 10, textvariable = fib, relief = 'sunken')
label.grid(row = 0, column = 1, rowspan = 2, sticky = 'nsew')
root.mainloop()
I expect the next number to appear every 1.2 seconds, but I just get 1 and nothing else.

TypeError: must be real number, not Entry

I am creating a code to create a calculator but I keep on getting this error:
Traceback (most recent call last):
File "C:\Users\Monish Shah\AppData\Local\Programs\Python\Python36-
32\lib\tkinter\__init__.py", line 1702, in __call__
return self.func(*args)
File "C:\Users\Monish Shah\AppData\Local\Programs\Python\Python36-
32\monish-play\calc-completed-copy-for-editing-copy2.py", line 40, in click
Label (window, text = str(sqrt(n_textentry)), bg = "white") .grid(row = 13,
column = 0, sticky = N)
TypeError: must be real number, not Entry
Does anyone know why my code does not work? I don't really understand why it cannot be and entry since I am collecting the user's input? I was researching but I could not figure out how to correctly incorporate the user's input into the code.
Here is my code that I used:
from math import sqrt
from tkinter import *
window = Tk()
window.title("Welcome to Calculator ")
window.configure(background = "white")
Label (window, text = "Calculator", bg = "white") .grid(row = 0, column = 0,
sticky = N)
#to create the box for the first number and store it
Label (window, text = "Enter the first number", bg = "white") .grid(row = 1,
column = 0, sticky = N)
n_textentry = Entry(window, width = 10, bg = "white")
n_textentry.grid(row = 2, column = 0, sticky = N)
#to create the box for the second number
Label (window, text = "Enter the second number", bg = "white") .grid(row = 5,
column = 0, sticky = N)
m_textentry = Entry(window, width = 10, bg = "white")
m_textentry.grid(row = 6, column = 0, sticky = N)
#click function
def click():
n_textentry.get()
m_textentry.get()
operation_textentry.get()
if operation_textentry == 1:
result1 = Label (window, text = str(n_textentry + m_textentry), bg =
"white") .grid(row = 13, column = 0, sticky = N)
elif operation_textentry == 2:
Label (window, text = str(n_textentry - m_textentry), bg = "white")
.grid(row = 13, column = 0, sticky = N)
elif operation_textentry == 3:
Label (window, text = str(n_textentry * m_textentry), bg = "white")
.grid(row = 13, column = 0, sticky = N)
elif operation_textentry == 4:
Label (window, text = str(n_textentry / m_textentry), bg = "white")
.grid(row = 13, column = 0, sticky = N)
elif operation_textentry == 5:
Label (window, text = str(n_textentry ** m_textentry), bg = "white")
.grid(row = 13, column = 0, sticky = N)
else:
Label (window, text = str(sqrt(n_textentry)), bg = "white")
.grid(row = 13, column = 0, sticky = N)
# operation_textentry == 6:
# Label (window, text = str(sqrt(n_textentry)), bg = "white")
.grid(row = 13, column = 0, sticky = N)
#else:
# print("Invalid Operation ")
#to show list of options
Label (window, text = '''
Enter 1 for addition
Enter 2 for subtraction
Enter 3 for multiplication
Enter 4 for division
Enter 5 for exponentiation
Enter 6 for square root *This will only work for 1st choice*''', bg =
"white") .grid(row = 9, column = 0, sticky = W)
operation_textentry = Entry(window, width = 10, bg = "white")
operation_textentry.grid(row = 10, column = 0, sticky = N)
Button(window, text = "Submit", width = 6, command=click) .grid(row = 11,
column = 0, sticky = N)
There are a number of problems with this code:
You need to store the results of those get calls.
As suggested by Joel, you need to convert them to float or int.
You should create the results Label once at startup, and config the text in this callback, instead of creating a new Label every time the user hits Submit.
Instead of repeating all of the same code 6 times, just calculate a result in the elif chain, and then use it at the end.
I already explained most of this in my answer to your previous question.
The result should look something like this:
result_label = Label(window, text = str(n_textentry ** m_textentry), bg = "white")
result_label.grid(row = 13, column = 0, sticky = N)
def click():
n = int(n_textentry.get())
m = int(m_textentry.get())
operation = int(operation_textentry.get())
if operation == 1:
result = n+m
elif operation == 2:
result = n-m
elif operation == 3:
result = n*m
elif operation == 4:
result = n/m
elif operation == 5:
result = n**m
else:
result = "Invalid Operation"
result_label.config(text=str(result))
As I mentioned before, you probably want some error handling for the case where the user leaves one of the entries blank, or inputs text instead of a number, or divides by zero, etc. The simplest way to do this with a try: around the whole click function:
def click():
try:
n = int(n_textentry.get())
# etc.
except Exception as e:
result_label.config(text=repr(e))
Try converting user input to a float or int. It looks like you're trying to apply a mathematical operation on user input (the Entry object), but mathematical operations are not supported for Entry objects.

Using Python to update labels Tkinter

I am in the process of making a Hangman type game. So far, I have written the CLI version which works well, i'm just porting it over to create a GUI.
I have become stuck :-(. The program is not complete, and there's still more to do but I have two issues. The first is the label update.
When a letter is chosen, it creates enough dashes for the letter, and places this in a label called 'letter'.
When a user enters a letter, it replaces the dashes, however it then adds a new label next to the old label, instead, I would like to replace this label. I have tried using the .set() but this doesn't seem to work.
My second issue, and this is more of a logic error (I think), is that I wanted to keep track of the letters entered so that I could compare this to newly entered letters and alert the user. This works well, however when a letter has been entered it will warn the user even if its the first time it has been typed.
Here's the code:
import tkinter
from tkinter import *
from tkinter import messagebox
import random
guesses = 8
def play():
print("play game")
wordList = ["talking", "dollar","choice", "famous", "define", "features"]
wordChoice = random.choice(wordList)
print(wordChoice)
wordLength = (len(wordChoice))
print(wordLength)
guessedLetters = []
dashes = []
def enterLetter():
print("Guess")
global guesses
print(guessedLetters)
while guesses != 0:
guess = entry.get().lower()
if len(guess) >1:
messagebox.showinfo("Error","Sorry, only one letter at a time")
entry.delete("0","end")
return
elif guess.isalpha() == False:
messagebox.showinfo("Error","Letters only please")
entry.delete("0","end")
return
elif guess in guessedLetters:
messagebox.showinfo("Error","You have already used the letter")
entry.delete("0","end")
return
guessedLetters.append(guess)
print(guessedLetters)
print(guesses)
count = 0
for i in range(wordLength):
if wordChoice[i] == guess:
dashes[i] = guess
count = count +1
letter = Label(play, text = dashes, font = ("Arial",20)).grid(row = 2, column = i+1,padx = 10, pady =10)
if count == 0:
guesses -= 1
if guesses == 0:
print("You have ran out of guesses!")
print("The word was:",wordChoice)
###### Play Game GUI
play = Toplevel()
play.title("Play Hangman!")
label = Label(play, text="HANGMAN", font = ("Arial",16)).grid(row = 0)
label = Label(play, text="Enter your guess >").grid(row = 3, column = 0)
for i in range(wordLength):
dashes.append("-")
letter = Label(play, text = dashes, font = ("Arial",20)).grid(row = 2, column = i+1,padx = 10, pady =10)
entry = Entry(play)
entry.grid(row = 3, column = 1, columnspan = wordLength)
enterButton = Button(play, text = "Enter Guess", width = 15, command = enterLetter).grid(row = 3, column = (wordLength+2))
label = Label(play, text = "Letter used: ").grid(row = 4, columnspan = 2)
label = Label(play, text = "").grid(row= 4, columnspan = 6)
def scores():
print("check scores")
def howToPlay():
print("how to play")
####### Main Menu
root = Tk()
root.geometry("500x300")
root.title("HANGMAN")
label = Label(root, text="HANGMAN", font = ("Arial",30)).grid(row = 0, columnspan = 3)
label = Label(root, text = "Option 1 :", font = ("Arial",12)).grid(row = 1, column = 1)
playButton = Button(root, text = "Play Game", width = 15, command = play).grid(row = 1, column = 2)
label = Label(root, text = "Option 2 :", font = ("Arial",12)).grid(row = 2, column = 1)
instructionsButton = Button(root, text = "How to play", width = 15, command = howToPlay).grid(row = 2, column = 2)
label = Label(root, text = "Option 3 :", font = ("Arial",12)).grid(row = 3, column = 1)
scoresButton = Button(root, text = "View Scores", width = 15, command = scores).grid(row = 3, column = 2)
label = Label(root, text = "Option 4 :", font = ("Arial",12)).grid(row = 4, column = 1)
exitButton = Button(root, text = "Exit", width = 15, command = exit).grid(row = 4, column = 2)
root.mainloop()
You need to configure the Label, not recreate it.
Why do you use a while-loop in enter_letter? Its just run when the Button is clicked, it needs to be an if guesses > 0:
Your program did not terminate when the right word was entered; I added this.
Code:
import tkinter
from tkinter import *
from tkinter import messagebox
import random
guesses = 8
letter = None
def play():
global letter
print("play game")
wordList = ["talking", "dollar","choice", "famous", "define", "features"]
wordChoice = random.choice(wordList)
print(wordChoice)
wordLength = (len(wordChoice))
print(wordLength)
guessedLetters = []
dashes = []
play = Toplevel()
play.title("Play Hangman!")
label = Label(play, text="HANGMAN", font = ("Arial",16)).grid(row = 0)
label = Label(play, text="Enter your guess >").grid(row = 3, column = 0)
for i in range(wordLength):
dashes.append("-")
letter = Label(play, text = dashes, font = ("Arial",20))
letter.grid(row = 2, column = i+1,padx = 10, pady =10)
print(letter)
def enterLetter():
print("Guess")
global guesses, letter
print(guessedLetters)
if guesses != 0:
guess = entry.get().lower()
if len(guess) >1:
messagebox.showinfo("Error","Sorry, only one letter at a time")
return
elif guess.isalpha() == False:
messagebox.showinfo("Error","Letters only please")
return
elif guess in guessedLetters:
messagebox.showinfo("Error","You have already used the letter")
return
entry.delete("0","end")
guessedLetters.append(guess)
#print(guessedLetters)
#print(guesses)
print(dashes)
count = 0
for i in range(wordLength):
if wordChoice[i] == guess:
dashes[i] = guess
count += 1
letter.configure(text = dashes)
if count == 0:
guesses -= 1
if "".join(dashes) == wordChoice:
print("succsess!")
play.destroy()
return
if guesses == 0:
print("You have ran out of guesses!")
print("The word was:",wordChoice)
###### Play Game GUI
entry = Entry(play)
entry.grid(row = 3, column = 1, columnspan = wordLength)
enterButton = Button(play, text = "Enter Guess", width = 15, command = enterLetter).grid(row = 3, column = (wordLength+2))
label = Label(play, text = "Letter used: ").grid(row = 4, columnspan = 2)
label = Label(play, text = "").grid(row= 4, columnspan = 6)
def scores():
print("check scores")
def howToPlay():
print("how to play")
####### Main Menu
root = Tk()
root.geometry("500x300")
root.title("HANGMAN")
label = Label(root, text="HANGMAN", font = ("Arial",30)).grid(row = 0, columnspan = 3)
label = Label(root, text = "Option 1 :", font = ("Arial",12)).grid(row = 1, column = 1)
playButton = Button(root, text = "Play Game", width = 15, command = play).grid(row = 1, column = 2)
label = Label(root, text = "Option 2 :", font = ("Arial",12)).grid(row = 2, column = 1)
instructionsButton = Button(root, text = "How to play", width = 15, command = howToPlay).grid(row = 2, column = 2)
label = Label(root, text = "Option 3 :", font = ("Arial",12)).grid(row = 3, column = 1)
scoresButton = Button(root, text = "View Scores", width = 15, command = scores).grid(row = 3, column = 2)
label = Label(root, text = "Option 4 :", font = ("Arial",12)).grid(row = 4, column = 1)
exitButton = Button(root, text = "Exit", width = 15, command = exit).grid(row = 4, column = 2)
root.mainloop()
I hope this helps you.
The reason you cant update your label is because you haven't stored it in any variable. The grid, pack and place functions of the Label object and of all other widgets returns None, therefore when you call:
letter = Label(play, text = dashes, font = ("Arial",20)).grid(row = 2, column = i+1,padx = 10, pady =10)
your label cannot be accessed by variable letter. To fix this you should split it like so:
letter = Label(play, text = dashes, font = ("Arial",20))
letter.grid(row = 2, column = i+1,padx = 10, pady =10)
To update text of that label, you can call .configure(text = 'new text') on it.
# letter = Label(play, text = dashes, font = ("Arial",20)).grid(row = 2, column = i+1,padx = 10, pady =10) #
letter.configure(text = dashes)
As for your second issue, i think you confused while loop and if statement in the function enterLetter. It's called once per click and you only need to check one time if player has ran out of guesses.
if guesses != 0:
...
elif guesses == 0:
....

How do you say "If button is pressed, then do this" in python tkinter?

I am trying to say "if the thing that you are trying to press equal for is an integer, then do what i have, but if it is a string, then print either "cos(whatever number)", "tan(whatever number)", or "sin(whatever number)". Please help clarify how to do this. Thank you.
from tkinter import*
root = Tk()
def btnPress(num):
global result
result = result+str(num)
equation.set(result)
def equalPress():
global result
total = str(eval(result))
equation.set(total)
result = total
def clear():
global result
result = ""
equation.set("")
result = ""
btnWidth = 5
equation = StringVar()
equation.set("Enter your equation.")
calculation = Label(root, textvariable = equation, width = btnWidth*4)
calculation.grid(row = 0, column = 0, columnspan = 4)
Button0 = Button(root, text = "0", command=lambda:btnPress(0), width = btnWidth)
Button0.grid(row = 4, column = 2)
Button1 = Button(root, text = "1", command=lambda:btnPress(1), width = btnWidth)
Button1.grid(row = 1, column = 1)
Button2 = Button(root, text = "2", command=lambda:btnPress(2), width = btnWidth)
Button2.grid(row = 1, column = 2)
Button3 = Button(root, text = "3", command=lambda:btnPress(3), width = btnWidth)
Button3.grid(row = 1, column = 3)
Button4 = Button(root, text = "4", command=lambda:btnPress(4), width = btnWidth)
Button4.grid(row = 2, column = 1)
Button5 = Button(root, text = "5", command=lambda:btnPress(5), width = btnWidth)
Button5.grid(row = 2, column = 2)
Button6 = Button(root, text = "6", command=lambda:btnPress(6), width = btnWidth)
Button6.grid(row = 2, column = 3)
Button7 = Button(root, text = "7", command=lambda:btnPress(7), width = btnWidth)
Button7.grid(row = 3, column = 1)
Button8 = Button(root, text = "8", command=lambda:btnPress(8), width = btnWidth)
Button8.grid(row = 3, column = 2)
Button9 = Button(root, text = "9", command=lambda:btnPress(9), width = btnWidth)
Button9.grid(row = 3, column = 3)
Button10 = Button(root, text = "tan", command=lambda:btnPress("tan "), width = btnWidth)
Button10.grid(row = 3, column = 0)
Button11 = Button(root, text = "sin", command=lambda:btnPress("sin "), width = btnWidth)
Button11.grid(row = 2, column = 0)
Button12 = Button(root, text = "cos", command=lambda:btnPress("cos "), width = btnWidth)
Button12.grid(row = 1, column = 0)
plus = Button(root, text = "+", command = lambda:btnPress("+"), width = btnWidth)
plus.grid(row = 1, column = 4)
plus = Button(root, text = "-", command = lambda:btnPress("-"), width = btnWidth)
plus.grid(row = 2, column = 4)
plus = Button(root, text = "*", command = lambda:btnPress("*"), width = btnWidth)
plus.grid(row = 3, column = 4)
plus = Button(root, text = "/", command = lambda:btnPress("/"), width = btnWidth)
plus.grid(row = 4, column = 4)
plus = Button(root, text = ".", command = lambda:btnPress("."), width = btnWidth)
plus.grid(row = 4, column = 1)
if ==
equal = Button(root, text = "=", command = equalPress, width = btnWidth)
equal.grid(row = 4, column = 3)
equal = Button(root, text = "C", command = clear, width = btnWidth)
equal.grid(row = 4, column = 0)
root.mainloop()
Simply check for the contents of num:
def btnPress(num):
global result
if num == "tan ":
result = tan(float(result))
elif num == "cos ":
result = tan(float(result))
...
else:
result = result+str(num)

Categories

Resources