display of minus and plus in tkinter calculator - python

Now I'm making calculator with python and tkinter, but i had one little problem. Minus and plus displaing in a really strange way and I don't think that it's looks cool.Look at Entry in which calculation should happend
So, I just want to know how can i centered them (move minus a little more higher).
My code:
from tkinter import *
root = Tk()
root.title("Calculator")
root.resizable(width=False, height=False)
# Main variables
equation = StringVar()
equation.set("0")
equa=""
# Program functions
def ButtonPress(num):
global equa
equa = equa + str(num)
equation.set(equa)
# Widgets
calculation = Entry(root, textvariable=equation, font=("Arial", 15,"bold"),
bd=12, insertwidth=4, width=15)
calculation.grid(columnspan=4)
# Numbers
button1 = Button(root, text='1', command=lambda: ButtonPress(1), bg="gainsboro",bd=3, padx=12, pady=5, font=("Arial", 13, "bold"))
button1.grid(row=1, column=0, sticky=W)
button2 = Button(root, text='2', command=lambda:ButtonPress(2),bg="gainsboro",bd=3, padx=12, pady=5, font=("Arial", 13, "bold"))
button2.grid(row=1, column=1, sticky=W)
button3 = Button(root, text='3', command=lambda: ButtonPress(3),bg="gainsboro",bd=3, padx=12, pady=5, font=("Arial", 13, "bold"))
button3.grid(row=1, column=2, sticky=W)
button4 = Button(root, text='4', command=lambda: ButtonPress(4),bg="gainsboro",bd=3, padx=12, pady=5, font=("Arial", 13, "bold"))
button4.grid(row=2, column=0, sticky=W)
button5 = Button(root, text='5', command=lambda: ButtonPress(5),bg="gainsboro",bd=3, padx=12, pady=5, font=("Arial", 13, "bold"))
button5.grid(row=2, column=1, sticky=W)
button6 = Button(root, text='6', command=lambda: ButtonPress(6),bg="gainsboro",bd=3, padx=12, pady=5, font=("Arial", 13, "bold"))
button6.grid(row=2, column=2, sticky=W)
button7 = Button(root, text='7', command=lambda: ButtonPress(7),bg="gainsboro",bd=3, padx=12, pady=5, font=("Arial", 13, "bold"))
button7.grid(row=3, column=0, sticky=W)
button8 = Button(root, text='8', command=lambda: ButtonPress(8),bg="gainsboro",bd=3, padx=12, pady=5, font=("Arial", 13, "bold"))
button8.grid(row=3, column=1, sticky=W)
button9 = Button(root, text='9', command=lambda: ButtonPress(9),bg="gainsboro",bd=3, padx=12, pady=5, font=("Arial", 13, "bold"))
button9.grid(row=3, column=2, sticky=W)
button0 = Button(root, text='0', command=lambda: ButtonPress(0),bg="gainsboro",bd=3, padx=12, pady=5, font=("Arial", 13, "bold"))
button0.grid(row=4, column=1, sticky=W)
button_plus = Button(root, text='+', command=lambda: ButtonPress('+'),bg="gainsboro",bd=3, padx=12, pady=5, font=("Arial", 13, "bold"))
button_plus.grid(row=1, column=3, sticky=W)
button_minus = Button(root, text='-', command=lambda: ButtonPress('-'),bg="gainsboro",bd=3, padx=14, pady=5, font=("Arial", 13, "bold"))
button_minus.grid(row=2, column=3, sticky=W)
root.mainloop()

The simplest solution is to choose a monospaced font, e.g. you could try:
calculation = Entry(root, textvariable=equation, font=("Courier", 15,"bold"),
bd=12, insertwidth=4, width=15)
calculation.grid(columnspan=4)

Related

tkinter callback event to retrieve value from Entry and write to text file

*First of all, i am trying to create a register system saved into textfile(not real system, i know its not safe to write into textfiles) Created the GUI and then i defined multiple functions which is Menu, register and submit. Submit function is nested and inside register function. The problem is when i nested the functions it doesn't write into textfiles, but when i delete the register function, it works.
When i press register and write something in the Entry box, it didnt record to the text files, i have been scratching my head as to find what the error in my code is.
Edit: I now have put a picture to get better understanding. the blue window is main menu and its the mainloop and the yellow window is appearing when i click register button.*
picture of what should be my app
from tkinter import *
def Register():
def Submit():
elev = open("bruker.txt","a", encoding="utf-8")
elev.write(brukeridinc.get()+"\n")
elev.write(fornavn.get()+"\n")
elev.write(etternavn.get()+"\n")
register = Tk()
register.geometry("500x500")
register.configure(bg="yellow")
register.title("Bibliotekapp Login")
label_brukerid = Label(register, text="Brukerid:", bg="white", font=("Arial", 25))
label_brukerid.grid(row=1, column=0, padx=5, pady=5, sticky=E)
label_fornavn = Label(register, text="Fornavn:", bg="white", font=("Arial", 25))
label_fornavn.grid(row=2, column=0, padx=5, pady=5, sticky=E)
label_etternavn = Label(register, text="Etternavn:", bg="white", font=("Arial", 25))
label_etternavn.grid(row=3, column=0, padx=5, pady=5, sticky=E)
brukeridinc= StringVar()
entry_brukerid= Entry(register, width=10, textvariable=brukeridinc, font=("Arial", 25))
entry_brukerid.grid(row=1, column=1, padx=5, pady=5, sticky=W)
fornavn= StringVar()
entry_fornavn= Entry(register, width=10, textvariable=fornavn, font=("Arial", 25))
entry_fornavn.grid(row=2, column=1, padx=5, pady=5, sticky=W)
etternavn= StringVar()
entry_etternavn= Entry(register, width=10, textvariable=etternavn, font=("Arial", 25))
entry_etternavn.grid(row=3, column=1, padx=5, pady=5, sticky=W)
button_resultat= Button(register, text="Enter", command=Submit, height=2, width=15)
button_resultat.grid(row=4, column=1, padx=5, pady=5, sticky=W)
informasjon= StringVar()
entry_informasjon= Entry(register, width=24, textvariable=informasjon, state="readonly", font=("Arial", 25))
entry_informasjon.grid(row=5, column=1, padx=5, pady=5, sticky=W)
#GUI
start = Tk()
start.geometry("500x500")
start.configure(bg="lightblue")
start.title("Bibliotekapp")
button_register = Button(start, text="Register", bg="white", font=("Arial", 25), command=Register)
button_register.grid(row=5, column=5, padx=5, pady=5, sticky=E)
start.mainloop()
I do not understand how you write code. You are using too many duplicates. I will not going to explain to you. In line 51 and 54 , I changed command to None. You can replace it.
Here is code:
from tkinter import *
start = Tk()
start.geometry("800x500")
start.configure(bg="lightblue")
start.title("Bibliotekapp")
def menu():
start.geometry("500x500")
start.title("Bibliotekapp")
start.configure(bg="red")
def Register():
start.geometry("800x800")
start.configure(bg="yellow")
start.title("Bibliotekapp Login")
def Submit():
elev = open("bruker.txt", "r")
sjekk = elev.read()
if brukeridinc.get() in sjekk:
informasjon.set("User already exists")
else:
elev.close()
elev = open("bruker.txt", "a")
elev.write(brukeridinc.get() + "")
elev.write(fornavn.get() + "")
elev.write(etternavn.get() + "\n")
elev.close()
informasjon.set("You are started")
####### GUI ############
label_brukerid = Label(start, text="Brukerid:", bg="white", font=("Arial", 25))
label_brukerid.grid(row=1, column=0, padx=5, pady=5, sticky=E)
label_fornavn = Label(start, text="Fornavn:", bg="white", font=("Arial", 25))
label_fornavn.grid(row=2, column=0, padx=5, pady=5, sticky=E)
label_etternavn = Label(start, text="Etternavn:", bg="white", font=("Arial", 25))
label_etternavn.grid(row=3, column=0, padx=5, pady=5, sticky=E)
brukeridinc= StringVar()
entry_brukerid= Entry(start, width=10, textvariable=brukeridinc, font=("Arial", 25))
entry_brukerid.grid(row=1, column=1, padx=5, pady=5, sticky=W)
fornavn= StringVar()
entry_fornavn= Entry(start, width=10, textvariable=fornavn, font=("Arial", 25))
entry_fornavn.grid(row=2, column=1, padx=5, pady=5, sticky=W)
etternavn= StringVar()
entry_etternavn= Entry(start, width=10, textvariable=etternavn, font=("Arial", 25))
entry_etternavn.grid(row=3, column=1, padx=5, pady=5, sticky=W)
button_start = Button(start, text="start", bg="white", font=("Arial", 25), command=Submit)
button_start.grid(row=5, column=5, padx=5, pady=5, sticky=E)
button_login = Button(start, text="Login", bg="white", font=("Arial", 25), command=menu)
button_login.grid(row=6, column=5, padx=5, pady=5, sticky=E)
button_lån = Button(start, text="Lån", bg="white", font=("Arial", 25), command=Register)
button_lån.grid(row=7, column=5, padx=5, pady=5, sticky=E)
informasjon= StringVar()
entry_informasjon= Entry(start, width=24, textvariable=informasjon, state="readonly", font=("Arial", 25))
entry_informasjon.grid(row=5, column=1, padx=5, pady=5, sticky=W)
start.mainloop()
Result:
Result Red:
Result for yellow:

Why my Scrollbar with grid, canvas and frame isn't working

I don't know why my Scrollbar doesn't work. I tried to look for answers on different IT pages, but could not find a satisfactory answer.
I have to use Labels with the Grid manager, and maybe it's a problem that the binding should be with the Frame and not Canvas (I tried to do that but it doesn't work either).
But for my application this is exactly what I need. I need to apply some kind of table that will get values from an external .txt file and present it in Labels.
from tkinter import *
ws = Tk()
ws.title('PythonGuides')
ws.geometry("200x200")
frame = Frame(
ws,
width=100,
height=100
)
frame.pack(expand=True, fill=BOTH)
canvas=Canvas(
frame,
bg='#4A7A8C',
width=200,
height=200,
scrollregion=(0,0,700,700)
)
Label(canvas, text="Name", font=("Arial", 12, "italic")).grid(row=0, column=0)
Label(canvas, text="Name1", font=("Arial", 12, "italic")).grid(row=0, column=2)
Label(canvas, text="1", font=("Arial", 10, "bold")).grid(row=1, column=0)
Label(canvas, text="1").grid(row=1, column=1)
Label(canvas, text="1").grid(row=1, column=2)
Label(canvas, text="1", font=("Arial", 10, "bold")).grid(row=2, column=0)
Label(canvas, text="1").grid(row=2, column=1)
Label(canvas, text="1", font=("Arial", 10, "bold")).grid(row=3, column=0)
Label(canvas, text="1").grid(row=3, column=1)
Label(canvas, text="1", font=("Arial", 10, "bold")).grid(row=4, column=0)
Label(canvas, text="1").grid(row=4, column=1)
Label(canvas, text="1").grid(row=4, column=2)
Label(canvas, text="1", font=("Arial", 10, "bold")).grid(row=5, column=0)
Label(canvas, text="1").grid(row=5, column=1)
Label(canvas, text="1", font=("Arial", 10, "bold")).grid(row=6, column=0)
Label(canvas, text="1").grid(row=6, column=1)
Label(canvas, text="1", font=("Arial", 10, "bold")).grid(row=7, column=0)
Label(canvas, text="1").grid(row=7, column=1)
Label(canvas, text="1",font=("Arial", 10, "bold")).grid(row=8, column=0)
Label(canvas, text="1").grid(row=8, column=1)
Label(canvas, text="1").grid(row=8, column=2)
Label(canvas, text="1").grid(row=8, column=3)
Label(canvas, text="1", font=("Arial", 10, "bold")).grid(row=9, column=0)
Label(canvas, text="1").grid(row=9, column=1)
Label(canvas, text="1", font=("Arial", 10, "bold")).grid(row=10, column=0)
Label(canvas, text="1").grid(row=10, column=1)
Label(canvas, text="1", font=("Arial", 10, "bold")).grid(row=11, column=0)
Label(canvas, text="1").grid(row=11, column=1)
Label(canvas, text="1", font=("Arial", 10, "bold")).grid(row=12, column=0)
Label(canvas, text="1").grid(row=12, column=1)
Label(canvas, text="1").grid(row=12, column=2)
Label(canvas, text="1", font=("Arial", 10, "bold")).grid(row=13, column=0)
Label(canvas, text="1").grid(row=13, column=1)
Label(canvas, text="1", font=("Arial", 10, "bold")).grid(row=14, column=0)
Label(canvas, text="1").grid(row=14, column=1)
Label(canvas, text="1").grid(row=14, column=2)
Label(canvas, text="1", font=("Arial", 10, "bold")).grid(row=15, column=0)
Label(canvas, text="1").grid(row=15, column=1)
Label(canvas, text="1").grid(row=15, column=2)
Label(canvas, text="1", font=("Arial", 10, "bold")).grid(row=16, column=0)
Label(canvas, text="1").grid(row=16, column=1)
Label(canvas, text="1").grid(row=16, column=2)
Label(canvas, text="1", font=("Arial", 10, "bold")).grid(row=17, column=0)
Label(canvas, text="1").grid(row=17, column=1)
Label(canvas, text="1").grid(row=17, column=2)
Label(canvas, text="1", font=("Arial", 10, "bold")).grid(row=18, column=0)
Label(canvas, text="1").grid(row=18, column=1)
Label(canvas, text="1", font=("Arial", 10, "bold")).grid(row=19, column=0)
Label(canvas, text="1").grid(row=19, column=1)
Label(canvas, text="1").grid(row=19, column=2)
Label(canvas, text="1",font=("Arial", 10, "bold")).grid(row=20, column=0)
Label(canvas, text="1").grid(row=20, column=1)
Label(canvas, text="1", font=("Arial", 10, "bold")).grid(row=21, column=0)
Label(canvas, text="1").grid(row=21, column=1)
Label(canvas, text="1", font=("Arial", 10, "bold")).grid(row=22, column=0)
Label(canvas, text="1").grid(row=22, column=1)
Label(canvas, text="1", font=("Arial", 10, "bold")).grid(row=23, column=0)
Label(canvas, text="1").grid(row=23, column=1)
Label(canvas, text="1").grid(row=23, column=2)
Label(canvas, text="1", font=("Arial", 10, "bold")).grid(row=24, column=0)
Label(canvas, text="1").grid(row=24, column=1)
Label(canvas, text="1", font=("Arial", 10, "bold")).grid(row=25, column=0)
Label(canvas, text="1").grid(row=25, column=1)
Label(canvas, text="1", font=("Arial", 10, "bold")).grid(row=26, column=0)
Label(canvas, text="1").grid(row=26, column=1)
Label(canvas, text="1", font=("Arial", 10, "bold")).grid(row=27, column=0)
Label(canvas, text="1").grid(row=27, column=1)
Label(canvas, text="1", font=("Arial", 10, "bold")).grid(row=28, column=0)
Label(canvas, text="1").grid(row=28, column=1)
vertibar=Scrollbar(
frame,
orient=VERTICAL
)
vertibar.pack(side=RIGHT,fill=Y)
vertibar.config(command=canvas.yview)
canvas.config(width=500,height=400)
canvas.config(
yscrollcommand=vertibar.set
)
canvas.pack(expand=True,side=LEFT,fill=BOTH)
ws.mainloop()

Is there a way to run a function on something after it is written (python)

I am creating a tkinter app with python, and I am faced with a problem.
from tkinter import *
import math
root = Tk()
root.title("PyCalc")
root.resizable(False, False)
inpbox = Entry(root, width=40, borderwidth=5, font=("Courier New", 16))
inpbox.grid(row=0, column=0, columnspan=5, padx=0, pady=10)
def clicked(num):
inpbox.focus()
if num == 'C': inpbox.delete(0, END)
elif num == '=':
inpbox.icursor(END)
text = inpbox.get().replace('√', "math.sqrt").replace('^', '**')
inpbox.delete(0, END)
try: num = eval(text)
except: inpbox.insert(END, '?')
else:
try: inpbox.insert(inpbox.index(INSERT), float(num))
except: inpbox.insert(END, '?')
elif num == 'B':
inpbox.delete(inpbox.index(INSERT)-1, inpbox.index(INSERT))
else: inpbox.insert(inpbox.index(INSERT), num)
inpbox.focus()
button00 = Button(root, text='00', padx=17, pady=20, font=("Courier New", 32), command=lambda: clicked('00'))
button0 = Button(root, text='0', padx=30, pady=20, font=("Courier New", 32), command=lambda: clicked('0'))
button1 = Button(root, text='1', padx=30, pady=20, font=("Courier New", 32), command=lambda: clicked('1'))
button2 = Button(root, text='2', padx=30, pady=20, font=("Courier New", 32), command=lambda: clicked('2'))
button3 = Button(root, text='3', padx=30, pady=20, font=("Courier New", 32), command=lambda: clicked('3'))
button4 = Button(root, text='4', padx=30, pady=20, font=("Courier New", 32), command=lambda: clicked('4'))
button5 = Button(root, text='5', padx=30, pady=20, font=("Courier New", 32), command=lambda: clicked('5'))
button6 = Button(root, text='6', padx=30, pady=20, font=("Courier New", 32), command=lambda: clicked('6'))
button7 = Button(root, text='7', padx=30, pady=20, font=("Courier New", 32), command=lambda: clicked('7'))
button8 = Button(root, text='8', padx=30, pady=20, font=("Courier New", 32), command=lambda: clicked('8'))
button9 = Button(root, text='9', padx=30, pady=20, font=("Courier New", 32), command=lambda: clicked('9'))
buttonplus = Button(root, text='+', padx=30, pady=20, font=("Courier New", 32), command=lambda: clicked('+'))
buttonsub = Button(root, text='-', padx=30, pady=20, font=("Courier New", 32), command=lambda: clicked('-'))
buttonmult = Button(root, text='*', padx=30, pady=20, font=("Courier New", 32), command=lambda: clicked('*'))
buttondiv = Button(root, text='/', padx=30, pady=20, font=("Courier New", 32), command=lambda: clicked('/'))
buttonsqr = Button(root, text='√', padx=30, pady=20, font=("Courier New", 32), command=lambda: clicked('√('))
buttonsq = Button(root, text='x²', padx=17, pady=20, font=("Courier New", 32), command=lambda: clicked('^2'))
buttonpow = Button(root, text='xⁿ', padx=17, pady=20, font=("Courier New", 32), command=lambda: clicked('^'))
buttoneq = Button(root, text='=', padx=91, pady=20, font=("Courier New", 32), command=lambda: clicked('='))
buttonc = Button(root, text='C', padx=30, pady=20, font=("Courier New", 32), command=lambda: clicked('C'))
buttonb = Button(root, text='Õ', padx=11, pady=20, font=("Wingdings", 31), command=lambda: clicked('B'))
buttonfact = Button(root, text='x!', padx=17, pady=20, font=("Courier New", 32), command=lambda: clicked('!'))
buttonlp = Button(root, text='(', padx=30, pady=20, font=("Courier New", 32), command=lambda: clicked('('))
buttonrp = Button(root, text=')', padx=30, pady=20, font=("Courier New", 32), command=lambda: clicked(')'))
buttonsq.grid(row=1, column=4)
buttonplus.grid(row=1, column=3)
button9.grid(row=1, column=2)
button8.grid(row=1, column=1)
button7.grid(row=1, column=0)
buttonpow.grid(row=2, column=4)
buttonsub.grid(row=2, column=3)
button6.grid(row=2, column=2)
button5.grid(row=2, column=1)
button4.grid(row=2, column=0)
buttonsqr.grid(row=3, column=4)
buttonmult.grid(row=3, column=3)
button3.grid(row=3, column=2)
button2.grid(row=3, column=1)
button1.grid(row=3, column=0)
buttonfact.grid(row=4, column=4)
buttondiv.grid(row=4, column=3)
buttonb.grid(row=4, column=2)
button00.grid(row=4, column=1)
button0.grid(row=4, column=0)
buttonc.grid(row=5, column=4)
buttoneq.grid(row=5, column=2, columnspan=2)
buttonrp.grid(row=5, column=1)
buttonlp.grid(row=5, column=0)
root.mainloop()
When the exclamation mark is clicked a ! is shown. But it has no effect on the equation and results in an error (which leaves a question mark on the screen)
For aesthetic purposes I want to avoid adding a !( to the equation or a fact(; it would take too much space, and it's not the way you .
In theory, if I entered 3! the program would output 6.
I have tried:
Doing setattr(int, 'factorial', fact) where fact is def fact(self): return math.factorial(self) (math had been imported in the program already) just returns the error:
File "<pyshell#0>", line 1, in <module>
setattr(int, 'factorial', fact)
TypeError: can't set attributes of built-in/extension type 'int'
I had hoped that I could add to the .replace chain in the third row of the clicked() function: text = inpbox.get().replace('√', "math.sqrt").replace('^', '**').replace('!', '.factorial()'), but since python won't let me add that attribute to numbers it was fruitless.
trying int.__setattr__() yielded around the same results.

In my calculator GUI with python tkinter, the 'Error' message in the entry widget is not automatically removing when i am writing the expression again

I've made a simple calculator GUI in Python using tkinter and everything is working perfectly.
I have added +, -, / and * functionalities only.
I have added a functionality that if someone types something that can't be evaluated, say 6w+8, then it shows an Error message in the Entry widget.
I want that Error message to disappear automatically when I start typing again from keyboard or from the buttons I've made in the GUI (even if I move the insertion cursor when Error message shows and then type).
It is disappearing when I press my GUI buttons and the text of that button appearing on the screen that is what I want. But the problem is that the Error message is not disappearing if I press a key of the keyboard (not GUI buttons).
Here is the code:
from tkinter import *
root = Tk()
root.title('Calculator')
root.geometry('790x680+350+7')
def calc_operation(event):
if entered_data.get() == 'Error':
input_screen.delete(0, END)
pressed_button_value = event.widget.cget('text')
cursor_index = input_screen.index(INSERT)
input_screen.insert(cursor_index, pressed_button_value)
def result(event=None):
final_expression = ''
for character in entered_data.get():
if character.lower() == 'x':
character = '*'
elif character == '÷':
character = '/'
final_expression += character
# Error handling
try:
answer = eval(final_expression)
if answer.__ceil__() == answer:
answer = int(answer)
entered_data.set(answer)
input_screen.icursor(len(entered_data.get()))
except:
if entered_data.get() == '':
entered_data.set('')
else:
entered_data.set('Error')
input_screen.icursor(len(entered_data.get()))
def clear(event=None):
input_screen.delete(0, END)
def backspace():
cursor_index = input_screen.index(INSERT)
if cursor_index == 0:
pass
else:
input_screen.delete(cursor_index-1)
# Binding keyboard buttons
root.bind('<Return>', result)
root.bind('<Delete>', clear)
# Input screen
entered_data = StringVar()
input_screen = Entry(root, font='helvetica 45', bd=4, relief=SUNKEN, textvariable=entered_data, justify=RIGHT)
input_screen.pack(pady=40, padx=20, fill=X)
input_screen.focus()
# Buttons for the numbers
frame = Frame(root)
frame.pack(pady=50)
button7 = Button(frame, text='7', font='helvetica 34', width=4, relief=RAISED, bd=4, bg='green')
button7.grid()
button7.bind('<Button-1>', calc_operation)
button8 = Button(frame, text='8', font='helvetica 34', width=4, relief=RAISED, bd=4, bg='green')
button8.grid(row=0, column=1)
button8.bind('<Button-1>', calc_operation)
button9 = Button(frame, text='9', font='helvetica 34', width=4, relief=RAISED, bd=4, bg='green')
button9.grid(row=0, column=2)
button9.bind('<Button-1>', calc_operation)
button4 = Button(frame, text='4', font='helvetica 34', width=4, relief=RAISED, bd=4, bg='green')
button4.grid(row=1, column=0)
button4.bind('<Button-1>', calc_operation)
button5 = Button(frame, text='5', font='helvetica 34', width=4, relief=RAISED, bd=4, bg='green')
button5.grid(row=1, column=1)
button5.bind('<Button-1>', calc_operation)
button6 = Button(frame, text='6', font='helvetica 34', width=4, relief=RAISED, bd=4, bg='green')
button6.grid(row=1, column=2)
button6.bind('<Button-1>', calc_operation)
button1 = Button(frame, text='1', font='helvetica 34', width=4, relief=RAISED, bd=4, bg='green')
button1.grid(row=2, column=0)
button1.bind('<Button-1>', calc_operation)
button2 = Button(frame, text='2', font='helvetica 34', width=4, relief=RAISED, bd=4, bg='green')
button2.grid(row=2, column=1)
button2.bind('<Button-1>', calc_operation)
button3 = Button(frame, text='3', font='helvetica 34', width=4, relief=RAISED, bd=4, bg='green')
button3.grid(row=2, column=2)
button3.bind('<Button-1>', calc_operation)
button0 = Button(frame, text='0', font='helvetica 34', relief=RAISED, bd=4, bg='green')
button0.grid(row=3, column=0, columnspan=2, sticky=NSEW)
button0.bind('<Button-1>', calc_operation)
# Buttons for operations
division_button = Button(frame, text='÷', font='helvetica 34', width=7, relief=RAISED, bd=4, bg='red', fg='white')
division_button.grid(row=0, column=3)
division_button.bind('<Button-1>', calc_operation)
multiplication_button = Button(frame, text='x', font='helvetica 22 bold', relief=RAISED, bd=4, bg='red', fg='white')
multiplication_button.grid(row=1, column=3, sticky=NSEW)
multiplication_button.bind('<Button-1>', calc_operation)
addition_button = Button(frame, text='+', font='helvetica 34', width=7, relief=RAISED, bd=4, bg='red', fg='white')
addition_button.grid(row=2, column=3)
addition_button.bind('<Button-1>', calc_operation)
subtraction_button = Button(frame, text='-', font='helvetica 34', width=7, relief=RAISED, bd=4, bg='red', fg='white')
subtraction_button.grid(row=3, column=3)
subtraction_button.bind('<Button-1>', calc_operation)
# Buttons for the symbols
decimal_button = Button(frame, text='.', font='helvetica 34', width=4, relief=RAISED, bd=4, bg='red', fg='white')
decimal_button.grid(row=3, column=2)
decimal_button.bind('<Button-1>', calc_operation)
backspace_button = Button(frame, text='←', font='helvetica 34', width=7, command=backspace, relief=RAISED, bd=4, bg='red', fg='white')
backspace_button.grid(row=0, column=4)
clear_button = Button(frame, text='Clear', font='helvetica 19 bold', command=clear, relief=RAISED, bd=4, bg='red', fg='white')
clear_button.grid(row=1, column=4, sticky=NSEW)
equal_button = Button(frame, text='=', font='helvetica 34', width=7, command=result, relief=RAISED, bd=4, bg='red', fg='white')
equal_button.grid(row=2, column=4)
exit_button = Button(frame, text='Exit', font='helvetica 19 bold', command=root.destroy, relief=RAISED, bd=4, bg='red', fg='white')
exit_button.grid(row=3, column=4, sticky=NSEW)
root.mainloop()
I also tried to bind the keys. Here is what I did:
def calc_operation2(event):
cursor_index = input_screen.index(INSERT)
input_screen.delete(cursor_index-1)
if entered_data.get() == 'Error':
input_screen.delete(0, END)
input_screen.insert(cursor_index, event.char)
root.bind('<Key>', calc_operation2)
and it is working. But it creates a new problem that when I press backspace key, it removed the Error message and displaying a weird rectangle shape in Entry widget.
Please help me resolve this problem.
Does this help You solve the issue (You still have to keep the other check in the calc_func):
from tkinter import Tk, Entry
root = Tk()
entry = Entry(root)
entry.pack()
entry.insert(0, 'Error')
entry.bind('<Key>', lambda x: entry.delete(0, 'end') if entry.get() == 'Error' else None)
root.mainloop()
here is another way:
from tkinter import Tk, Entry
def check(event):
if event.widget.get() == 'Error':
event.widget.delete(0, 'end')
root = Tk()
entry = Entry(root)
entry.pack()
entry.insert(0, 'Error')
entry.bind('<Key>', check)
root.mainloop()

7x4 grid displaying 1 button 28 times instead of 28 different buttons being displayed. Why?

I would like to display the 28 buttons below in a 7x4 self-resizing grid. As it stands now, the equal button is being displayed 28 times. I need all 28 different buttons to display in the 7x4 grid. I have all 28 different buttons configured with the row_index and col_index variables, but it's still not working. If you could help steer me in the right direction, I'd greatly appreciate it.
...
enter code here
class Calculator(ttk.Frame)
def init(self):
root = Tk()
root.title("TK's Calculator")
root.geometry("700x500")
Grid.rowconfigure(root, 0, weight=1)
Grid.columnconfigure(root, 0, weight=1)
root.resizable(True, True)
frame1 = Frame(root, bg="#80c1ff", bd=5)
frame1.grid(row=0, column=0, sticky="nsew")
for row_index in range(7):
Grid.rowconfigure(frame1, row_index, weight=1)
for col_index in range(4):
Grid.columnconfigure(frame1, col_index, weight=1)
buttonMC = Button(frame1, text="MC", height=3, width=5, command=MC)
buttonMC.grid(row=row_index, column=col_index, sticky="nsew")
buttonMC.configure(width=7, height=3, font=("Verdana", 10, "bold"), fg="Navy")
MRButton = Button(frame1, text="MR", height=3, width=5, command=MR)
MRButton.grid(row=row_index, column=col_index, sticky="nsew")
MRButton.configure(width=7, height=3, font=("Verdana", 10, "bold"), fg="Navy")
MPlusButton = Button(frame1, text="M+", height=3, width=5, command=MPlus)
MPlusButton.grid(row=row_index, column=col_index, sticky="nsew")
MPlusButton.configure(width=7, height=3, font=("Verdana", 10, "bold"), fg="Navy")
percentButton = Button(frame1, text="%", height=3, width=5, command=percent)
percentButton.grid(row=row_index, column=col_index, sticky="nsew")
percentButton.configure(width=7, height=3, font=("Verdana", 10, "bold"), fg="Navy")
buttonModulo = Button(frame1, text="Mod", height=3, width=5, command=modulo)
buttonModulo.grid(row=row_index, column=col_index, sticky="nsew")
buttonModulo.configure(width=7, height=3, font=("Verdana", 10, "bold"), fg="Navy")
buttonSqRoot = Button(frame1, text="√(x)", height=3, width=5, command=squareRootOfx)
buttonSqRoot.grid(row=row_index, column=col_index, sticky="nsew")
buttonSqRoot.configure(width=7, height=3, font=("Verdana", 10, "bold"), fg="Navy")
button2ndPower = Button(frame1, text="x²", height=3, width=5, command=powerOf2)
button2ndPower.grid(row=row_index, column=col_index, sticky="nsew")
button2ndPower.configure(width=7, height=3, font=("Verdana", 10, "bold"), fg="Navy")
buttonOneOverX = Button(frame1, text="1/x", height=3, width=5, command=oneOverX)
buttonOneOverX.grid(row=row_index, column=col_index, sticky="nsew")
buttonOneOverX.configure(width=7, height=3, font=("Verdana", 10, "bold"), fg="Navy")
buttonCE = Button(frame1, text="CE", height=3, width=5, command=clearEntry)
buttonCE.grid(row=row_index, column=col_index, sticky="nsew")
buttonCE.configure(width=7, height=3, font=("Verdana", 10, "bold"), fg="Navy")
buttonClear = Button(frame1, text="C", height=3, width=5, command=clear)
buttonClear.grid(row=row_index, column=col_index, sticky="nsew")
buttonClear.configure(width=7, height=3, font=("Verdana", 10, "bold"), fg="Navy")
buttonDelete = Button(frame1, text="del", height=3, width=5, command=delete)
buttonDelete.grid(row=row_index, column=col_index, sticky="nsew")
buttonDelete.configure(width=7, height=3, font=("Verdana", 10, "bold"), fg="Navy")
buttonDivide = Button(frame1, text="/", height=3, width=5, command=divide)
buttonDivide.grid(row=row_index, column=col_index, sticky="nsew")
buttonDivide.configure(width=7, height=3, font=("Verdana", 10, "bold"), fg="Navy")
button7 = Button(frame1, text="7", height=3, width=5, command=lambda: buttonClick(7))
button7.grid(row=row_index, column=col_index, sticky="nsew")
button7.configure(width=7, height=3, font=("Verdana", 10, "bold"), fg="Navy")
button8 = Button(frame1, text="8", height=3, width=5, command=lambda: buttonClick(8))
button8.grid(row=row_index, column=col_index, sticky="nsew")
button8.configure(width=7, height=3, font=("Verdana", 10, "bold"), fg="Navy")
button9 = Button(frame1, text="9", height=3, width=5, command=lambda: buttonClick(9))
button9.grid(row=row_index, column=col_index, sticky="nsew")
button9.configure(width=7, height=3, font=("Verdana", 10, "bold"), fg="Navy")
buttonMultiply = Button(frame1, text="x", height=3, width=5, command=multiply)
buttonMultiply.grid(row=row_index, column=col_index, sticky="nsew")
buttonMultiply.configure(width=7, height=3, font=("Verdana", 10, "bold"), fg="Navy")
button4 = Button(frame1, text="4", height=3, width=5, command=lambda: buttonClick(4))
button4.grid(row=row_index, column=col_index, sticky="nsew")
button4.configure(width=7, height=3, font=("Verdana", 10, "bold"), fg="Navy")
button5 = Button(frame1, text="5", height=3, width=5, command=lambda: buttonClick(5))
button5.grid(row=row_index, column=col_index, sticky="nsew")
button5.configure(width=7, height=3, font=("Verdana", 10, "bold"), fg="Navy")
button6 = Button(frame1, text="6", height=3, width=5, command=lambda: buttonClick(6))
button6.grid(row=row_index, column=col_index, sticky="nsew")
button6.configure(width=7, height=3, font=("Verdana", 10, "bold"), fg="Navy")
buttonMinus = Button(frame1, text="-", height=3, width=5, command=subtract)
buttonMinus.grid(row=row_index, column=col_index, sticky="nsew")
buttonMinus.configure(width=7, height=3, font=("Verdana", 10, "bold"), fg="Navy")
button1 = Button(frame1, text="1", height=3, width=5, command=lambda: buttonClick(1))
button1.grid(row=row_index, column=col_index, sticky="nsew")
button1.configure(width=7, height=3, font=("Verdana", 10, "bold"), fg="Navy")
button2 = Button(frame1, text="2", height=3, width=5, command=lambda: buttonClick(2))
button2.grid(row=row_index, column=col_index, sticky="nsew")
button2.configure(width=7, height=3, font=("Verdana", 10, "bold"), fg="Navy")
button3 = Button(frame1, text="3", height=3, width=5, command=lambda: buttonClick(3))
button3.grid(row=row_index, column=col_index, sticky="nsew")
button3.configure(width=7, height=3, font=("Verdana", 10, "bold"), fg="Navy")
buttonAdd = Button(frame1, text="+", height=3, width=5, command=add)
buttonAdd.grid(row=row_index, column=col_index, sticky="nsew")
buttonAdd.configure(width=7, height=3, font=("Verdana", 10, "bold"), fg="Navy")
buttonPlusMinus = Button(frame1, text="+/-", height=3, width=5, command=plusMinus)
buttonPlusMinus.grid(row=row_index, column=col_index, sticky="nsew")
buttonPlusMinus.configure(width=7, height=3, font=("Verdana", 10, "bold"), fg="Navy")
buttonZero = Button(frame1, text="0", height=3, width=5, command=lambda: buttonClick(0))
buttonZero.grid(row=row_index, column=col_index, sticky="nsew")
buttonZero.configure(width=7, height=3, font=("Verdana", 10, "bold"), fg="Navy")
buttonDecimal = Button(frame1, text=".", height=3, width=5, command=decimal)
buttonDecimal.grid(row=row_index, column=col_index, sticky="nsew")
buttonDecimal.configure(width=7, height=3, font=("Verdana", 10, "bold"), fg="Navy")
buttonEquals = Button(frame1, text="=", height=3, width=5, command=equals)
buttonEquals.grid(row=row_index, column=col_index, sticky="nsew")
buttonEquals.configure(width=7, height=3, font=("Verdana", 10, "bold"), fg="Navy")
frame2 = Frame(root, bg="lightgrey", bd=5)
frame2.grid(row=0, column=1, sticky="nsew")
historyButton = Button(frame2, text="Clear History", fg="Navy", bg="#80c1ff", command=clearhistory)
historyButton.configure(font=("Verdana", 12, "bold"), fg="Navy")
historyButton.place(x=5, y=40)
memoryButton = Button(frame2, text="Memory", bg="#80c1ff", fg="navy", command=memory)
memoryButton.configure(font=("Verdana", 12, "bold"), fg="Navy")
memoryButton.place(x=135, y=40)
self.result = Entry(frame2, width=24, borderwidth=3)
self.result.configure(font=("Verdana", 14))
self.result.grid(row=0, column=0, pady=5)
root.mainloop()
Calculator()
...
You are placing all of your controls at row=row_index, column=col_index. By the time that runs, row_index is 7 and col_index is 4. You need to hard-code the locations for all of those buttons, as in row_index=0, col_index=3. Then it will work. I'm looking at it now:

Categories