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

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

Related

Python code simple Calculator with Tkinter i'am noob in python. Is it posible?

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

Show the label among the selections, depending on the radio-button clicked in TKinter?

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

TypeError: clickButton() missing 1 required positional argument: 'number'

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

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

I am using the .grid command but its not lined up correctly and is not straight

I am writing a program that makes a calculator with a GUI and I'm using tkinter but when I use the .grid command and assign it to a column it doesn't line up correctly and there is a bit of a gap between them... I am using python 3.7. Any ideas please.
I have tried using the grid and pack commands. I know there is the place method but not sure the exact co-ordinates I have to use so any idea on how to use the grid method correctly.
from tkinter import *
#from Tkinter import *
root = Tk()
root.title("Calculator")
#========================Screen=======================
Display = Entry(root, fg='white', bg='green',
justify='right', bd=25)
Display.grid(columnspan=4)
#=======================Row1==========================
b7 = Button(root, padx=30, pady=15, bd=8, fg='black', text='7').grid(row=1, column=0)
b8 = Button(root, padx=30, pady=15, bd=8, fg='black', text='8').grid(row=1, column=1)
b9 = Button(root, padx=30, pady=15, bd=8, fg='black', text='9').grid(row=1, column=2)
clear = Button(root, padx=30, pady=15, bd=8, fg='black', text='C', highlightbackground='green', highlightthickness=0.1).grid(row=1, column=3)
#=======================Row2==========================
b4 = Button(root, padx=30, pady=15, bd=8, fg='black', text='4').grid(row=2, column=0)
b5 = Button(root, padx=30, pady=15, bd=8, fg='black', text='5').grid(row=2, column=1)
b6 = Button(root, padx=30, pady=15, bd=8, fg='black', text='6').grid(row=2, column=2)
plus = Button(root, padx=30, pady=15, bd=8, fg='black', text='+', highlightbackground='orange', highlightthickness=0.1).grid(row=2, column=3)
#=======================Row3==========================
b1 = Button(root, padx=30, pady=15, bd=8, fg='black', text='1').grid(row=3, column=0)
b2 = Button(root, padx=30, pady=15, bd=8, fg='black', text='2').grid(row=3, column=1)
b3 = Button(root, padx=30, pady=15, bd=8, fg='black', text='3').grid(row=3, column=2)
minus = Button(root, padx=30, pady=15, bd=8, fg='black', text='-', highlightbackground='orange', highlightthickness=0.1).grid(row=3, column=3)
root.mainloop()
I want the C, + and - to be lined up in the same column

Categories