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

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()

Related

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.

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:

NOT using pack still getting error: cannot use geometry manager grid inside . which already has slaves managed by pack

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) <----------------

the text in the Button function doesn't appear in my phyton Tkinter. I'm using a MacBooks. anyone has any idea how to fix my code?

can someone help me ou. I couldn't seem to figure out why the button's text doesn't appear on my interface window. im using a Mac OS.
'''
from Tkinter import *
root = Tk()
root.title("Simple Calculator")
e = Entry(root, width=35, borderwidth= 5)
e.grid(row=0, column=0, columnspan=3, padx=10, pady=10)
def button_add():
return
button_1 = Button(root, text= "1",fg="red", padx = 40, pady =20, command=button_add)
button_2 = Button(root, text= "2", fg="red",padx = 40, pady =20, command=button_add)
button_3 = Button(root, text= "3", fg="red",padx = 40, pady =20, command=button_add)
button_4 = Button(root, text= "4", fg="red",padx = 40, pady =20, command=button_add)
button_5 = Button(root, text= "5",fg="red", padx = 40, pady =20, command=button_add)
button_6 = Button(root, text= "6", fg="red",padx = 40, pady =20, command=button_add)
button_7 = Button(root, text= "7", fg="red",padx = 40, pady =20, command=button_add)
button_8 = Button(root, text= "8", fg="red",padx = 40, pady =20, command=button_add)
button_9 = Button(root, text= "9",fg="red", padx = 40, pady =20, command=button_add)
# put buttons on the screen
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)
root.mainloop()
'''
please help. I don't know why the button's text doesn't appear on the interface window. it only appears as white.
There are many syntax error in your program.
for eg the correct syntax for Button is button_1 = tkinter.Button(root, text='1')
kindy go thru the documentation of tkinter.
Note: you program have many indentation warning of unnecessary spaces. kindly work on that.
Try the below code.
try:
import tkinter
except ImportError: # for python 2 user
import Tkinter as tkinter
root = tkinter.Tk()
root.title("Simple Calculator")
e = tkinter.Entry(root, width=35, borderwidth= 5)
e.grid(row=0, column=0, columnspan=3, padx=10, pady=10)
def button_add():
return
button_1 = tkinter.Button(root, text="1",fg="red", padx=40, pady=20, command=button_add)
button_2 = tkinter.Button(root, text="2", fg="red", padx=40, pady=20, command=button_add)
button_3 = tkinter.Button(root, text="3", fg="red", padx=40, pady=20, command=button_add)
button_4 = tkinter.Button(root, text="4", fg="red", padx =40, pady=20, command=button_add)
button_5 = tkinter.Button(root, text="5", fg="red", padx=40, pady=20, command=button_add)
button_6 = tkinter.Button(root, text="6", fg="red", padx=40, pady=20, command=button_add)
button_7 = tkinter.Button(root, text="7", fg="red", padx=40, pady=20, command=button_add)
button_8 = tkinter.Button(root, text="8", fg="red", padx=40, pady=20, command=button_add)
button_9 = tkinter.Button(root, text="9", fg="red", padx=40, pady=20, command=button_add)
# put buttons on the screen
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)
root.mainloop()

display of minus and plus in tkinter calculator

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)

Categories