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

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

Related

Python Tkinter making space between buttons (vertical)

import tkinter as tk
root = tk.Tk()
root.title("Calculator")
def button_write():
return
data = tk.Entry(root, width=30, borderwidth=10, font="bold 20", )
data.grid(column=0, row=0, columnspan=4, padx=10, pady=10)
buton1 = tk.Button(root,text="1", padx=40, pady=25, command=button_write)
buton2 = tk.Button(root,text="2", padx=40, pady=25, command=button_write)
buton3 = tk.Button(root,text="3", padx=40, pady=25, command=button_write)
buton4 = tk.Button(root,text="4", padx=40, pady=25, command=button_write)
buton5 = tk.Button(root,text="5", padx=40, pady=25, command=button_write)
buton6 = tk.Button(root,text="6", padx=40, pady=25, command=button_write)
buton7 = tk.Button(root,text="7", padx=40, pady=25, command=button_write)
buton8 = tk.Button(root,text="8", padx=40, pady=25, command=button_write)
buton9 = tk.Button(root,text="9", padx=40, pady=25, command=button_write)
buton0 = tk.Button(root,text="0", padx=40, pady=25, command=button_write)
buton1.grid(row= 1,column=0)
buton2.grid(row= 1,column=1)
buton3.grid(row= 1,column=2)
buton4.grid(row= 1,column=3)
buton5.grid(row= 2,column=0)
buton6.grid(row= 2,column=1)
buton7.grid(row= 2,column=2)
buton8.grid(row= 2,column=3)
buton9.grid(row= 3,column=1)
buton0.grid(row= 3,column=2)
root.mainloop()
I am trying to build a calculator but I don't know how to make vertical space between buttons. I used pady for that but I didn't exactly understand how it works so I think it doesn't work for that don't worry about that 9 and 0 it is because of photo
You need to add the padx and pady args to the grid() calls, e.g.:
buton1.grid(row=1, column=0, padx=10, pady=10)
Setting the padding on the Button widgets just sets their internal padding (makes them wider / taller)
Bonus Round
def button_write(button):
print(button) # do whatever you need to do here
for i, n in enumerate(range(9, -1, -1)):
btn = tk.Button(root, text=n, command=lambda btn=n: button_write(btn))
y, x = divmod(i, 3)
btn.grid(row=y, column=x, padx=10, pady=10)

How to remove 2 buttons by pressing one?

I want to remove the buttons "play" and "help" by pressing just on button "play". How can I do that? I need that the button "play" destroy himself and in addition destroy the button "help"
This is my code:
from tkinter import *
import tkinter.messagebox
from random import *
window = Tk()
window.title("Simon")
window.geometry("300x200")
label = Label(window, text="Simon Game!", font=("Ariel", 80),
bg="CadetBlue3")
label.pack()
window.configure(bg="CadetBlue3")
def destroy(button):
def inner():
button.destroy()
return inner
def click_help_button():
tkinter.messagebox.showinfo("Instructions", "The device
creates a series of tones and lights and requires a user to
repeat the sequence. If the user succeeds, the series becomes
progressively longer and more complex. Once the user fails, the
game is over")
help_btn = Button(window, width=12, height=2, text="Help",
bg="grey",font=("Ariel", 18), command=click_help_button)
help_btn.pack(side='bottom')
help_btn.place(x=800, y=150)
start_btn = Button(window, width=12, height=2, text="Play",
bg="grey", font=("Ariel", 18), command=destroy(help_btn))
start_btn.pack(pady=10)
start_btn.place(x=525, y=150)
start_btn.config(command=destroy(start_btn))
red_btn = Button(window, width=35, height=15, bg='red')
red_btn.place(x=495, y=270)
green_btn = Button(window, width=35, height=15, bg='green')
green_btn.place(x=750, y=270)
blue_btn = Button(window, width=35, height=15, bg='blue')
blue_btn.place(x=495, y=495)
yellow_btn = Button(window, width=35, height=15, bg='yellow')
yellow_btn.place(x=750, y=495)
window.mainloop()
Thanks to those who help!
I would make a click_play_button function
def click_play_button():
start_btn.destroy()
help_btn.destroy()
And call on it when pressing the play button
start_btn = Button(window, width=12, height=2, text="Play",
bg="grey", font=("Ariel", 18), command=click_play_button)
start_btn.pack(pady=10)
start_btn.place(x=525, y=150)
Below also worked...
def destroy(button):
button.destroy()
def click_help_button():
tkinter.messagebox.showinfo("Instructions", "The device")
help_btn = Button(window, width=12, height=2, text="Help",
bg="grey", font=("Ariel", 18), command=click_help_button)
help_btn.pack(side='bottom')
help_btn.place(x=800, y=150)
start_btn = Button(window, width=12, height=2, text="Play",
bg="grey", font=("Ariel", 18), command=lambda: [destroy(help_btn), destroy(start_btn)])
start_btn.pack(pady=10)
start_btn.place(x=525, y=150)
red_btn = Button(window, width=35, height=15, bg='red')
red_btn.place(x=495, y=270)
green_btn = Button(window, width=35, height=15, bg='green')
green_btn.place(x=750, y=270)
blue_btn = Button(window, width=35, height=15, bg='blue')
blue_btn.place(x=495, y=495)
yellow_btn = Button(window, width=35, height=15, bg='yellow')
yellow_btn.place(x=750, y=495)
window.mainloop()

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

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

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

Categories