I am learning about the Tkinter and have created a Calculator using it. But now I want to write test cases so that I can verify things. But I don't know how to write test cases in Tkinter. I have tried to write the code but it seems like there is some error in it.
My Code:
calculator.py
from ast import operator
from distutils import text_file
from tkinter import *
from matplotlib import test
from numpy import number
def gui():
root = Tk()
root.title("Simple Calculator")
root.configure(bg="#1b4f72")
return root
textField = Entry(width=25, highlightthickness=5, bg="#d6eaf8", font="Cambria 11")
textField.configure(highlightbackground = "#1b4f72", highlightcolor= "#1b4f72")
textField.grid(row=0, column=0, columnspan=4)
root = gui()
def button_click(number):
value = textField.get()
textField.delete(0, END)
textField.insert(0, str(value)+str(number))
def buttonClear():
textField.delete(0, END)
def add():
global first
global operators
operators = "+"
first = textField.get()
textField.delete(0, END)
def subtract():
global first
global operators
operators = "-"
first = textField.get()
textField.delete(0, END)
def divide():
global first
global operators
operators = "/"
first = textField.get()
textField.delete(0, END)
def multiply():
global first
global operators
operators = "*"
first = textField.get()
textField.delete(0, END)
def equal():
second = textField.get()
textField.delete(0, END)
try:
if operators=="+":
textField.insert(0, int(first)+int(second))
elif operators=="-":
textField.insert(0, int(first)-int(second))
elif operators=="*":
textField.insert(0, int(first)*int(second))
elif operators=="/":
try:
textField.insert(0, int(first)/int(second))
except ZeroDivisionError:
textField.insert(0, "Zero Division")
except ValueError:
if operators=="+":
textField.insert(0, float(first)+float(second))
elif operators=="-":
textField.insert(0, float(first)-float(second))
elif operators=="*":
textField.insert(0, float(first)*float(second))
elif operators=="/":
try:
textField.insert(0, float(first)/float(second))
except ZeroDivisionError:
textField.insert(0, "Zero Division")
button_1 = Button(root, text="1", bg= '#2980b9', command=lambda: button_click(1), pady=10, padx=20, font="Cambria 11")
button_2 = Button(root, text="2", bg= '#2980b9', command=lambda: button_click(2), pady=10, padx=20, font="Cambria 11")
button_3 = Button(root, text="3", bg= '#2980b9', command=lambda: button_click(3), pady=10, padx=20, font="Cambria 11")
button_4 = Button(root, text="4", bg= '#2980b9', command=lambda: button_click(4), pady=10, padx=20, font="Cambria 11")
button_5 = Button(root, text="5", bg= '#2980b9', command=lambda: button_click(5), pady=10, padx=20, font="Cambria 11")
button_6 = Button(root, text="6", bg= '#2980b9', command=lambda: button_click(6), pady=10, padx=20, font="Cambria 11")
button_7 = Button(root, text="7", bg= '#2980b9', command=lambda: button_click(7), pady=10, padx=20, font="Cambria 11")
button_8 = Button(root, text="8", bg= '#2980b9', command=lambda: button_click(8), pady=10, padx=20, font="Cambria 11")
button_9 = Button(root, text="9", bg= '#2980b9', command=lambda: button_click(9), pady=10, padx=20, font="Cambria 11")
button_0 = Button(root, text="0", bg= '#2980b9', command=lambda: button_click(0), pady=10, padx=20, font="Cambria 11")
button_dot = Button(root, text=".", bg= '#2980b9', command=lambda: button_click("."), pady=10, padx=22, font="Cambria 11")
buttonClear = Button(root, text="Clear", bg= '#2980b9', command=buttonClear, pady=10, padx=97, font="Cambria 11")
buttonAdd = Button(root, text="+", bg= '#2980b9', command=add, pady=10, padx=20, font="Cambria 11")
buttonSubtract = Button(root, text="-", bg= '#2980b9', command=subtract, pady=10, padx=21, font="Cambria 11")
buttonMultiply = Button(root, text="X", bg= '#2980b9', command=multiply, pady=10, padx=20, font="Cambria 11")
buttonDivide = Button(root, text="/", bg= '#2980b9', command=divide, pady=10, padx=20, font="Cambria 11")
buttonEqual = Button(root, text="=", bg= '#2980b9', command=equal, pady=10, padx=20, font="Cambria 11")
button_7.grid(row=1, column=0)
button_8.grid(row=1, column=1)
button_9.grid(row=1, column=2)
button_4.grid(row=2, column=0)
button_5.grid(row=2, column=1)
button_6.grid(row=2, column=2)
button_1.grid(row=3, column=0)
button_2.grid(row=3, column=1)
button_3.grid(row=3, column=2)
button_0.grid(row=4, column=0)
button_dot.grid(row=4, column=1)
buttonEqual.grid(row=4, column=2)
buttonAdd.grid(row=1, column=3)
buttonSubtract.grid(row=2, column=3)
buttonMultiply.grid(row=3, column=3)
buttonDivide.grid(row=4,column=3)
buttonClear.grid(row=5, column=0, columnspan=4)
root.mainloop()
test_calculator.py
from calculator import *
def test_add():
button_click(4)
buttonAdd()
button_click(9)
buttonEqual()
assert textField.get() == 13
def test_subtract():
button_click(4)
buttonSubtract()
button_click(9)
buttonEqual()
assert textField.get() == -5
def test_multiply():
button_click(4)
buttonMultiply()
button_click(9)
buttonEqual()
assert textField.get() == 36
def test_divide():
button_click(9)
buttonDivide()
button_click(3)
buttonEqual()
assert textField.get() == 3
Any type of help will be appreciated.
Thanks
Related
I'm new to Python and Tkinter and am trying to make a window, that has a button, which when pressed, opens code for a Calculator in a new window, I have the calculator code ready and it works on its own, but when I copied it and tried to get it to work with the new window, it just opens an empty window.
The open new window code was made from this tutorial: Open a new Window with a button in Python-Tkinter.
The calculator was made from FreeCodeCamp.org's tkinter course, it had a lot of code that included e.get, thinking this was the problem I changed it to newWindow.
from tkinter import *
from tkinter.ttk import *
master = Tk()
master.geometry("200x200")
def openNewWindow():
global newWindow
newWindow = Toplevel(master)
newWindow.title("New Window")
newWindow = Entry(width=35, borderwidth=5)
newWindow.grid(row=0, column=0, columnspan=3, padx=10, pady=10)
def button_click(number):
current = newWindow.get()
newWindow.delete(0, END)
newWindow.insert(0, str(current) + str(number))
def button_clear():
newWindow.delete(0, END)
def button_add():
first_number = newWindow.get()
global f_num
global math
math = "addition"
f_num = int(first_number)
newWindow.delete(0, END)
def button_equal():
second_number = newWindow.get()
newWindow.delete(0, END)
if math == "addition":
newWindow.insert(0, f_num + int(second_number))
if math == "subtraction":
newWindow.insert(0, f_num - int(second_number))
if math == "multiplication":
newWindow.insert(0, f_num * int(second_number))
if math == "devision":
newWindow.insert(0, f_num / int(second_number))
def button_subtract():
first_number = newWindowe.get()
global f_num
global math
math = "subtraction"
f_num = int(first_number)
newWindow.delete(0, END)
def button_multiply():
first_number = newWindow.get()
global f_num
global math
math = "multiplication"
f_num = int(first_number)
newWindow.delete(0, END)
def button_devide():
first_number = newWindow.get()
global f_num
global math
math = "devision"
f_num = int(first_number)
newWindow.delete(0, END)
button_1 = Button(root, text="1", padx=40, pady=20, command=lambda: button_click(1))
button_2 = Button(root, text="2", padx=40, pady=20, command=lambda: button_click(2))
button_3 = Button(root, text="3", padx=40, pady=20, command=lambda: button_click(3))
button_4 = Button(root, text="4", padx=40, pady=20, command=lambda: button_click(4))
button_5 = Button(root, text="5", padx=40, pady=20, command=lambda: button_click(5))
button_6 = Button(root, text="6", padx=40, pady=20, command=lambda: button_click(6))
button_7 = Button(root, text="7", padx=40, pady=20, command=lambda: button_click(7))
button_8 = Button(root, text="8", padx=40, pady=20, command=lambda: button_click(8))
button_9 = Button(root, text="9", padx=40, pady=20, command=lambda: button_click(9))
button_0 = Button(root, text="0", padx=40, pady=20, command=lambda: button_click(0))
button_add = Button(root, text="+", padx=39, pady=20, command=button_add)
button_equal = Button(root, text="=", padx=91, pady=20, command=button_equal)
button_clear = Button(root, text="C", padx=91, pady=20, command=button_clear)
button_subtract = Button(root, text="-", padx=41, pady=20, command=button_subtract)
button_multiply = Button(root, text="*", padx=40, pady=20, command=button_multiply)
button_devide = Button(root, text="/", padx=41, pady=20, command=button_devide)
# Put the buttons on the screen
#myButton = Button(root, text="What is your name?", command=myClick)
button_1.grid(row=3, column=0)
button_2.grid(row=3, column=1)
button_3.grid(row=3, column=2)
button_4.grid(row=2, column=0)
button_5.grid(row=2, column=1)
button_6.grid(row=2, column=2)
button_7.grid(row=1, column=0)
button_8.grid(row=1, column=1)
button_9.grid(row=1, column=2)
button_0.grid(row=4, column=0)
button_clear.grid(row=4, column=1, columnspan=2)
button_add.grid(row=5, column=0)
button_equal.grid(row=5, column=1, columnspan=2)
button_subtract.grid(row=6, column=0)
button_multiply.grid(row=6, column=1)
button_devide.grid(row=6, column=2)
label = Label(master,
text ="Window")
label.pack(pady = 10)
btn = Button(master,
text ="Calculator",
command = openNewWindow)
btn.pack(pady = 10)
mainloop()
Watch the terminal when you're running your code. When the window stops responding, it usually means python encountered an error and it's printed in the terminal. Looks like you've still got some work to do to migrate your code over from being standalone - root is not defined anywhere. Also I removed from tkinter.ttk import * which got rid of a bunch of unknown options errors for me.
Without actually doing the whole thing (although I did and I learned something), here is a skeleton:
def openNewWindow():
# Toplevel window uses pack manager, not grid manager
newWindow = Toplevel(master)
newWindow.title(...)
# Create a frame as the only object in Toplevel.
# The frame will contain the other objects and use the grid manager.
frame = Frame(newWindow)
frame.pack()
# Create items in the frame and grid them
entry = Entry(frame, width=35)
entry.grid(row=0, ...)
button_1 = Button(frame, text="1", command=...)
button_1.grid(row=3, column=0, padx=4, pady=2)
button_2 = Button(...)
button_2.grid(...)
...
For manipulating the content of the text entry box:
entry.get()
entry.delete(0, END)
entry.insert(0, str)
import tkinter as tk
root = tk.Tk()
root.title("skaiciuotuvas")
e = tk.Entry(root, width=35, borderwidth=5)
e.grid(row=0, column=0,columnspan=3, padx=10, pady=15)
e.insert(0, 0)
Button action functions
def button_click(number):
e.delete(0, "end")
current = e.get()
e.delete(0, "end")
e.insert(0, str(current) + str(number))
def button_clear():
e.delete(0, "end")
this is simple calculiator that do num + num = res : need to get num + num + num ...etc = rez is it posible corecct this code
def button_add():
first_number = int(e.get())
global f_num
f_num = int(first_number)
e.delete(0, "end")
is it posible to do only with def button_add(): function?
def button_equal():
second_number = e.get()
e.delete(0, "end")
e.insert(0, int(second_number)+ f_num)
buttons this is spec num values for my projet
button1 = tk.Button(root, text="1", padx=40, pady=20, command=lambda: button_click(-30))
button2 = tk.Button(root, text="2", padx=40, pady=20, command=lambda: button_click(-20))
button3 = tk.Button(root, text="3", padx=40, pady=20, command=lambda: button_click(-18))
button4 = tk.Button(root, text="4", padx=40, pady=20, command=lambda: button_click(-16))
button5 = tk.Button(root, text="5", padx=40, pady=20, command=lambda: button_click(-15))
button6 = tk.Button(root, text="6", padx=40, pady=20, command=lambda: button_click(-12))
button7 = tk.Button(root, text="7", padx=40, pady=20, command=lambda: button_click(-7))
button8 = tk.Button(root, text="8", padx=40, pady=20, command=lambda: button_click(8))
button9 = tk.Button(root, text="9", padx=40, pady=20, command=lambda: button_click(9))
button10 = tk.Button(root, text="10", padx=37, pady=20, command=lambda: button_click(10))
button_15 = tk.Button(root, text="-15", padx=34, pady=20, command=lambda: button_click(-15))
button_add = tk.Button(root, text="+", padx=39, pady=20, command= button_add)
button_equal = tk.Button(root, text="=", padx=86, pady=20, command= button_equal)
button_clear = tk.Button(root, text="C", padx=39, pady=20, command=button_clear)
#put buttons on screen
button2.grid(row=3, column=2)
button3.grid(row=3, column=1)
button4.grid(row=3, column=0)
button5.grid(row=2, column=2)
button6.grid(row=2, column=1)
button7.grid(row=2, column=0)
button8.grid(row=1, column=2)
button9.grid(row=1, column=1)
button10.grid(row=1, column=0)
button_add.grid(row=4, column=2)
button_15.grid(row=5, column=0)
button1.grid(row=4, column=1)
button_equal.grid(row=5,column=1, columnspan=2)
button_clear.grid(row=4,column=0)
root.mainloop()
I am having a problem with, only having a single label to show in an instance of clicking the radio button. I want to show that label depending with the radio-button I clicked, I tried reading about pack/grid_forget, but I think I still don't understand it pretty well, or I am just doing it really wrong in my code.
from tkinter import *
root = Tk()
# I squeezed them inside a frame, just for aesthetic reasons.
btn_frame = LabelFrame(root, text='', padx=20, pady=20)
btn_frame.grid(row=0, column=0, columnspan=3, padx=10, pady=10)
# I use button widgets, instead of other widgets.Because I felt buttons are square, and they are easy
to visualize.
red_btn = Button(btn_frame, text='R', padx=40, pady=40)
blue_btn = Button(btn_frame, text='B', padx=40, pady=40)
green_btn = Button(btn_frame, text='G', padx=40, pady=40)
red_btn.grid(row=0, column=0)
blue_btn.grid(row=0, column=1)
green_btn.grid(row=0, column=2)
def rad_btnClick(value):
red_Btn_press_label = Label(root, text='You press R!', padx=40, bg='red')
blu_Btn_press_label = Label(root, text='You press B!', padx=40, bg='blue')
grn_Btn_press_label = Label(root, text='You press G!', padx=40, bg='green')
if value == 1:
red_Btn_press_label.grid(row=2, column=0,)
red_btn = Button(btn_frame, text='r', padx=40, pady=40, bg='red')
red_btn.grid(row=0, column=0)
blu_Btn_press_label.grid_forget()
grn_Btn_press_label.grid_forget()
elif value == 2:
blu_Btn_press_label.grid(row=2, column=1)
blue_btn = Button(btn_frame, text='b', padx=40, pady=40, bg='blue')
blue_btn.grid(row=0, column=1)
red_Btn_press_label.grid_forget()
grn_Btn_press_label.grid_forget()
elif value == 3:
grn_Btn_press_label.grid(row=2, column=2)
green_btn = Button(btn_frame, text='g', padx=40, pady=40, bg='green')
green_btn.grid(row=0, column=2)
red_Btn_press_label.grid_forget()
blu_Btn_press_label.grid_forget()
radiopress = IntVar()
# LABEL, VALUE, COLUMN
RAD_BUTTONS = [('RED BTN', 1, 0),
('BLU BTN', 2, 1),
('GRN BTN', 3, 2)]
for color, value, column in RAD_BUTTONS:
rad_btn = Radiobutton(root, text=color, variable=radiopress, value=value, command= lambda:rad_btnClick(radiopress.get()))
rad_btn.grid(row=1, column=column)
root.mainloop()
So I am new in Python, I've just recently understood the 4/5 core fundamentals in every prog-language.
And now I am trying to learn about GUI with python, with Tkinter. I think my code got a bit longer, for something so simple and small. You can lecture my anyway you want, I just want learn good practices, and work on bad habits I may be doing.
I figured out that I should make my widgets global, and just make an update function to display every time an instance of them is created.
I want to thank Tim Roberts for helping me.
from tkinter import *
root = Tk()
btn_frame = LabelFrame(root, text='', padx=20, pady=20)
btn_frame.grid(row=0, column=0, columnspan=3, padx=10, pady=10)
red_btn = Button(btn_frame, text='R', padx=40, pady=40)
blue_btn = Button(btn_frame, text='B', padx=40, pady=40)
green_btn = Button(btn_frame, text='G', padx=40, pady=40)
red_btn.grid(row=0, column=0)
blue_btn.grid(row=0, column=1)
green_btn.grid(row=0, column=2)
def rad_btnClick(value):
global rad_btn_press_label
global red_btn
global blue_btn
global green_btn
if value == 1:
rad_btn_press_label.grid_forget()
rad_btn_press_label = Label(root, text='You press R!', padx=40, bg='red')
red_btn = Button(btn_frame, text='R', padx=40, pady=40, bg='red')
blue_btn = Button(btn_frame, text='B', padx=40, pady=40)
green_btn = Button(btn_frame, text='G', padx=40, pady=40)
rad_btn_press_label.grid(row=2, column=0)
red_btn.grid(row=0, column=0)
blue_btn.grid(row=0, column=1)
green_btn.grid(row=0, column=2)
elif value == 2:
rad_btn_press_label.grid_forget()
rad_btn_press_label = Label(root, text='You press R!', padx=40, bg='blue')
red_btn = Button(btn_frame, text='R', padx=40, pady=40)
blue_btn = Button(btn_frame, text='B', padx=40, pady=40, bg='blue')
green_btn = Button(btn_frame, text='G', padx=40, pady=40)
rad_btn_press_label.grid(row=2, column=1)
red_btn.grid(row=0, column=0)
blue_btn.grid(row=0, column=1)
green_btn.grid(row=0, column=2)
elif value == 3:
rad_btn_press_label.grid_forget()
rad_btn_press_label = Label(root, text='You press G!', padx=40, bg='green')
red_btn = Button(btn_frame, text='R', padx=40, pady=40)
blue_btn = Button(btn_frame, text='B', padx=40, pady=40)
green_btn = Button(btn_frame, text='G', padx=40, pady=40, bg='green')
rad_btn_press_label.grid(row=2, column=2)
red_btn.grid(row=0, column=0)
blue_btn.grid(row=0, column=1)
green_btn.grid(row=0, column=2)
radiopress = IntVar()
# LABEL, VALUE, ROW, COLUMN
RAD_BUTTONS = [('RED BTN', 1, 1, 0),
('BLU BTN', 2, 1, 1),
('GRN BTN', 3, 1, 2)]
for color, value, row, column in RAD_BUTTONS:
rad_btn = Radiobutton(root, text=color, variable=radiopress, value=value, command=lambda: rad_btnClick(radiopress.get()))
rad_btn.grid(row=row, column=column)
rad_btn_press_label = Label(root, text='You press R!', padx=40,)
rad_btn_press_label.grid(row=2, column=0, )
rad_btn_press_label.grid_forget()
root.mainloop()
So I know we don't mix .pack and .grid and in my simple code I haven't packed but am using .grid but still getting error.
On the bottom, I have attached the error image that I am getting along with the code
Code:
`from tkinter import *
root = Tk()
root.title("Calculator")
# root.geometry('600x500')
def add():
print("works")
input_num1 = Entry(root)
input_num1.pack()
button_0 = Button(root, text="0", command=add)
button_1 = Button(root, text="1", command=add)
button_2 = Button(root, text="2", command=add)
button_3 = Button(root, text="3", command=add)
button_4 = Button(root, text="4", command=add)
button_5 = Button(root, text="5", command=add)
button_6 = Button(root, text="6", command=add)
button_7 = Button(root, text="7", command=add)
button_8 = Button(root, text="8", command=add)
button_9 = Button(root, text="9", command=add)
button_add = Button(root, text="+", command=add)
button_minus = Button(root, text="-", command=add)
button_div = Button(root, text="/", command=add)
button_multi = Button(root, text="X", command=add)
button_0.grid(row=4, column=0)
button_1.grid(row=3, column=0)
button_2.grid(row=3, column=1)
button_3.grid(row=3, column=2)
button_4.grid(row=2, column=0)
button_5.grid(row=2, column=1)
button_6.grid(row=2, column=2)
button_7.grid(row=1, column=0)
button_8.grid(row=1, column=1)
button_9.grid(row=1, column=2)
button_add.grid(row=1, column=3)
button_minus.grid(row=1, column=4)
button_div.grid(row=2, column=3)
button_multi.grid(row=2, column=4)
root.mainloop()
`
Error:
enter image description here
As it has already been noted, you are indeed using pack().
Try commenting out the offending line (or remove it completely) and setting input_num1 to occupy row 4, column 5 in the grid, like this:
input_num1 = Entry(root)
#input_num1.pack() <----------------
input_num1.grid(row=4, column=5) <----------------
I'm making a calculator in Python using Tkinter, and I'm getting an error:
TypeError: clickButton() missing 1 required positional argument: 'number'
Here's the code I've written:
from tkinter import *
root = Tk()
root.title("Calculator")
ent = Entry(root, width=35, borderwidth=5)
ent.grid(row=0, column=0, columnspan=3, padx=10, pady=10)
def clickButton(number):
current = ent.get()
ent.delete(0, END)
ent.insert(0, str(current) + str(number))
def clearButton():
ent.delete(0, END)
def button_add():
first_number = ent.get()
global f_num
global math
math = "addition"
f_num = int(first_number)
ent.delete(0, END)
def equalButton():
secondNum = ent.get()
ent.delete(0,END)
ent.insert(0, f_num + int(secondNum))
# this defines the buttons
button1 = Button(root, text="1", padx=40, pady=20, command=lambda: clickButton(1)) # the first button of the calculator
button2 = Button(root, text="2", padx=40, pady=20, command=lambda: clickButton(2)) # the second button of the calculator
button3 = Button(root, text="3", padx=40, pady=20, command=lambda: clickButton(3)) # the third button of the calculator
button4 = Button(root, text="4", padx=40, pady=20, command=lambda: clickButton(4)) # the fourth button of the calculator
button5 = Button(root, text="5", padx=40, pady=20, command=lambda: clickButton(5)) # the fifth button of the calculator
button6 = Button(root, text="6", padx=40, pady=20, command=lambda: clickButton(6)) # the sixth button of the calculator
button7 = Button(root, text="7", padx=40, pady=20, command=lambda: clickButton(7)) # the seventh button of the calculator
button8 = Button(root, text="8", padx=40, pady=20, command=lambda: clickButton(8)) # the eighth button of the calculator
button9 = Button(root, text="9", padx=40, pady=20, command=lambda: clickButton(9)) # the ninth button of the calculator
button0 = Button(root, text="0", padx=40, pady=20, command=lambda: clickButton(0)) # the tenth button of the calculator
button_addition = Button(root, text="+", padx=40, pady=20, command=clickButton) # addition button
button_equalSign = Button(root, text="=", padx=91, pady=20, command=equalButton) # button for the equal sign
button_clr = Button(root, text="C", padx=91, pady=20, command=lambda: clearButton()) #button for clearing whatever is written
# this puts the buttons on the screen
#row 3
button1.grid(row=3, column=0)
button2.grid(row=3, column=1)
button3.grid(row=3, column=2)
# row 2
button4.grid(row=2, column=0)
button5.grid(row=2, column=1)
button6.grid(row=2, column=2)
# row 1
button7.grid(row=1, column=0)
button8.grid(row=1, column=1)
button9.grid(row=1, column=2)
# sign buttons
button0.grid(row=4, column=0)
button_clr.grid(row=4, column=1, columnspan=2)
button_addition.grid(row=5, column=0)
button_equalSign.grid(row=5, column=1, columnspan=2)
root.mainloop()
I've been trying to fix this error for many hours.
P.S. I don't actually know which line the error is on, because it's saying that the error is on line 1705, even though the code is only 101 lines
In this line:
button_addition = Button(root, text="+", padx=40, pady=20, command=clickButton) # addition button
You are calling clickButton with no arguments, and in your function, you are requiring a number. You should use lambda, as you did with the other numbers (lines above), or call another function. By the look of your code, it should be button_add.
I fixed it this way:
myButtonSum = Button(root, text="+", padx=10, pady=5, command=lambda: buttonAdd("+"))