I'm new and slowly been trying to learn some of tkinter and I've found a few nice resources for basic projects but the explanations of the codes have been a bit lacking and I'm hoping someone can help me understand a bit of what I've scrapped together.
full code
from tkinter import *
#Create window and give it title
root = Tk()
root.title('Tip Calculator - Charlie')
#define the window sizes
window_width = 600
window_height = 400
#get the screen size
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
#solve for center of screen based on window & screen Size
center_x = int(screen_width/2 - window_width/2)
center_y = int(screen_height/2 - window_height/2)
#implement the above to position window width/height
root.geometry(f'{window_width}x{window_height} {center_x} {center_y}')
var = IntVar()
frame = LabelFrame(root,text = "Tip %")
frame.pack(pady=10)
r1 = Radiobutton(frame, text = 'no Tip', variable = var, value = 0)
r1.grid(column =0, row =1)
r2= Radiobutton(frame, text = '10 %', variable = var, value = 1)
r2.grid(column=0, row =2)
r3=Radiobutton(frame, text = '15 %', variable = var, value = 2)
r3.grid(column=1, row = 1)
r4= Radiobutton(frame, text = '20 %', variable = var, value =3)
r4.grid(column=1, row=2)
code in question
var = IntVar()
frame = LabelFrame(root,text = "Tip %")
frame.pack(pady=10)
r1 = Radiobutton(frame, text = 'no Tip', variable = var, value = 0)
r1.grid(column =4, row =1)
r2= Radiobutton(frame, text = '%', variable = var, value = 1)
r2.grid(column=4, row =2)
So I understand to an extent r1 is a variable name for the "Radiobutton", text is set to what pops up beside the button.
The question is what does variable = var, value = 1
and the first 3 lines i'm at a loss why is a variable called var being created and what does setting it = IntVar() do?
Is frame a widget? I assume pack is another version of positioning (grid) ?
CodePudding user response:
This page will give you a nice explanation radio widget
Var is being used to establish an integer variabel as opposed to string variable. (IntVar() vs StringVar()). Read through the explanation in that documentation and let me know if it still doesn't make sense. As for you question on pack() its just a fast method that literally packs the widget in the center and top if you don't specify anything else.
CodePudding user response:
variable = var
means that when the radio button is selected, the IntVar
in the var
variable should be given the value of this radio button. And the value
option specifies the value that should be used for each radio button.
So after you select the no Tip
radio button, var.get()
will be 0
. After you select the %
button, var.get()
will be 1
. That's how you find out which choice they made with the radio buttons.
Later in the code you can write:
if var.get() == 0:
tip = 0
elif var.get() == 1:
tip = 0.10 * total_price
elif var.get() == 2:
tip = 0.15 * total_price
elif var.get() == 3:
tip = 0.20 * total_price
price_with_tip = total_price tip
pack()
is a geometry manager, an alternative to grid()
and place()
. See this tutorial
CodePudding user response:
To answer your follow up question I think this would help you for how to handle the next steps.
from tkinter import *
def get_value():
tab = 50 # set to 50 but could get a value from entry if you want
choice = var.get()
print(choice) # print your choice for clarification
ten_percent = tab * .1 # math for tip
total = ten_percent tab
if choice == 1:
print("Your total plus tip is:", total)
print("Your tip amount was:", ten_percent)
#Create window and give it title
root = Tk()
root.title('Tip Calculator - Charlie')
#define the window sizes
window_width = 600
window_height = 400
#get the screen size
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
#solve for center of screen based on window & screen Size
center_x = int(screen_width/2 - window_width/2)
center_y = int(screen_height/2 - window_height/2)
#implement the above to position window width/height
root.geometry(f'{window_width}x{window_height} {center_x} {center_y}')
var = IntVar()
frame = LabelFrame(root,text = "Tip %")
frame.pack(pady=10)
r1 = Radiobutton(frame, text = 'no Tip', variable = var, value = 0)
r1.grid(column =0, row =1)
r2= Radiobutton(frame, text = '10 %', variable = var, value = 1)
r2.grid(column=0, row =2)
r3=Radiobutton(frame, text = '15 %', variable = var, value = 2)
r3.grid(column=1, row = 1)
r4= Radiobutton(frame, text = '20 %', variable = var, value =3)
r4.grid(column=1, row=2)
button = Button(root, text="Check", command= get_value)
button.pack()