Related
This question already has answers here:
Why does Tkinter image not show up if created in a function?
(5 answers)
Closed 1 year ago.
from tkinter import *
from tkinter import ttk
from PIL import ImageTk ,Image
win=tkinter.Toplevel()
wrapper=Frame(win, bd=4, relief=RIDGE, bg="crimson")
wrapper.place(x=0, y=80, width=465, height=625)
wrapper3=Frame(win, bd=4, relief=RIDGE, bg="crimson")
wrapper3.place(x=950, y=80, width=465, height=625)
wrapper3_title=Label(wrapper3, text="Selected Data", bg="crimson", fg="white", font=("times new roman",30,"bold"))
wrapper3_title.grid(row=0,column=0,padx=20, pady=10)
wrapper2=Frame(win, bd=4, relief=RIDGE, bg="crimson")
wrapper2.place(x=465, y=80, width=485, height=625)
ent8=StringVar()
def code():
btn1.destroy()
add=StringVar()
sub=StringVar()
pro=StringVar()
img=ImageTk.PhotoImage(Image.open("Amritsar.jpg"))
Label2= Label(wrapper2, image=img)
Label2.grid(row=0, column=0, padx=10, pady=5, sticky='w')
def Find():
add.set(float(ent00.get())+float(ent01.get()))
sub.set(float(ent00.get())-float(ent01.get()))
pro.set(float(ent00.get())*float(ent01.get()))
ent00=Entry(wrapper, width=15)
ent00.grid(row=4, column=1, padx=10, pady=10, sticky='w')
ent01=Entry(wrapper, width=15)
ent01.grid(row=5, column=1, padx=10, pady=10, sticky='w')
lbl8=Label(wrapper, text="Add", bg="crimson", fg="white", font=("times new roman",15,"bold")).grid(row=6, column=0, padx=20, pady=10, sticky='w')
ent8=Entry(wrapper, textvariable=add, width=15, state='readonly')
ent8.grid(row=6, column=1, padx=10, pady=10, sticky='w')
lbl15=Label(wrapper, text="Subtract", bg="crimson", fg="white", font=("times new roman",15,"bold")).grid(row=7, column=0, padx=20, pady=10, sticky='w')
ent15=Entry(wrapper, textvariable=sub, width=15, state='readonly')
ent15.grid(row=7, column=1, padx=10, pady=10, sticky='w')
lbl9=Label(wrapper, text="Product", bg="crimson", fg="white", font=("times new roman",15,"bold")).grid(row=8, column=0, padx=20, pady=10, sticky='w')
ent9=Entry(wrapper, textvariable=pro, width=15, state='readonly')
ent9.grid(row=8, column=1, padx=10, pady=10, sticky='w')
btn = Button(wrapper, text = 'Calculate', command=Find, bd = '5', width=15, height=2)
btn.grid(row=11, column=1, padx=20, pady=10)
def img():
if ent8.get()=="4":
img=ImageTk.PhotoImage(Image.open("Amritsar.jpg"))
Label2= Label(wrapper3, image=img)
Label2.grid(row=0, column=2, padx=10, pady=5, sticky='w')
print("Move ahead")
else:
print("Try again")
btn2 = Button(wrapper, text = 'Image', command=img, bd = '5', width=15, height=2)
btn2.grid(row=12, column=1, padx=20, pady=10)
btn1 = Button(wrapper, text = 'OPEN CODE', command=code, bd = '5', width=20, height=2)
btn1.grid(row=11, column=1, padx=20, pady=10)
win.geometry("1400x700+250+250")
win.mainloop()
Two images need to be shown on the tkinter. The one defined earlier in wrapper2, shows empty frame while the one that has to appear in wrapper3 after getting 4 as sum, does not appear at all. Moreover, the output printed is "Try again". Why it is so? When sum is 4 it has to show "Move ahead".
First of all, terrible names.
Both your function and your PhotoImage are named img. Rename the function to def add_img().
Second, looking at your code I have no idea what all the wrapper frames are for, why not name them according to what they are planned to hold? Same applies to all the widgets. Wouldn't calc_btn be a better name than btn? img_btn instead of btn2? Why do you need to read more than the name to know what something is?
Third, you have ent8 twice in your code. Once as Label and again as a StringVar.
Tkinter constantly refreshes your window so you need to save the image you are using.
Personally I would have done all of this in a class.
For right now, with your current code, just add
loaded_img = ImageTk.PhotoImage(Image.open("Amritsar.jpg")) before your functions and instead of using the variables you are using to open the image, just use Label(wrapper3, image=loaded_img)
As in:
win = Toplevel()
wrapper=Frame(win, bd=4, relief=RIDGE, bg="crimson")
wrapper.place(x=0, y=80, width=465, height=625)
wrapper3=Frame(win, bd=4, relief=RIDGE, bg="crimson")
wrapper3.place(x=950, y=80, width=465, height=625)
wrapper3_title=Label(wrapper3, text="Selected Data", bg="crimson", fg="white", font=("times new roman",30,"bold"))
wrapper3_title.grid(row=0,column=0,padx=20, pady=10)
wrapper2=Frame(win, bd=4, relief=RIDGE, bg="crimson")
wrapper2.place(x=465, y=80, width=485, height=625)
ent8=StringVar()
loaded_img = ImageTk.PhotoImage(Image.open("Amritsar.jpg"))
Edit
Here is the entire code:
from tkinter import *
from tkinter import ttk
from PIL import ImageTk ,Image
win=Toplevel()
wrapper=Frame(win, bd=4, relief=RIDGE, bg="crimson")
wrapper.place(x=0, y=80, width=465, height=625)
wrapper3=Frame(win, bd=4, relief=RIDGE, bg="crimson")
wrapper3.place(x=950, y=80, width=465, height=625)
wrapper3_title=Label(wrapper3, text="Selected Data", bg="crimson", fg="white", font=("times new roman",30,"bold"))
wrapper3_title.grid(row=0,column=0,padx=20, pady=10)
wrapper2=Frame(win, bd=4, relief=RIDGE, bg="crimson")
wrapper2.place(x=465, y=80, width=485, height=625)
ent8=StringVar()
loaded_img = ImageTk.PhotoImage(Image.open("Amritsar.jpg"))
add_strvar = StringVar()
sub_strvar = StringVar()
pro_strvar = StringVar()
def code():
btn1.destroy()
Label2= Label(wrapper2, image=loaded_img)
Label2.grid(row=0, column=0, padx=10, pady=5, sticky='w')
def Find():
add_strvar.set(float(ent00.get())+float(ent01.get()))
sub_strvar.set(float(ent00.get())-float(ent01.get()))
pro_strvar.set(float(ent00.get())*float(ent01.get()))
ent00=Entry(wrapper, width=15)
ent00.grid(row=4, column=1, padx=10, pady=10, sticky='w')
ent01=Entry(wrapper, width=15)
ent01.grid(row=5, column=1, padx=10, pady=10, sticky='w')
lbl8=Label(wrapper, text="Add", bg="crimson", fg="white", font=("times new roman",15,"bold")).grid(row=6, column=0, padx=20, pady=10, sticky='w')
ent8=Entry(wrapper, textvariable=add_strvar, width=15, state='readonly')
ent8.grid(row=6, column=1, padx=10, pady=10, sticky='w')
lbl15=Label(wrapper, text="Subtract", bg="crimson", fg="white", font=("times new roman",15,"bold")).grid(row=7, column=0, padx=20, pady=10, sticky='w')
ent15=Entry(wrapper, textvariable=sub_strvar, width=15, state='readonly')
ent15.grid(row=7, column=1, padx=10, pady=10, sticky='w')
lbl9=Label(wrapper, text="Product", bg="crimson", fg="white", font=("times new roman",15,"bold")).grid(row=8, column=0, padx=20, pady=10, sticky='w')
ent9=Entry(wrapper, textvariable=pro_strvar, width=15, state='readonly')
ent9.grid(row=8, column=1, padx=10, pady=10, sticky='w')
btn = Button(wrapper, text = 'Calculate', command=Find, bd = '5', width=15, height=2)
btn.grid(row=11, column=1, padx=20, pady=10)
def add_img():
if add_strvar.get() == "4.0":
Label2= Label(wrapper3, image=loaded_img)
Label2.grid(row=0, column=2, padx=10, pady=5, sticky='w')
print("Move ahead")
else:
print("Try again")
btn2 = Button(wrapper, text = 'Image', command=add_img, bd = '5', width=15, height=2)
btn2.grid(row=12, column=1, padx=20, pady=10)
btn1 = Button(wrapper, text = 'OPEN CODE', command=code, bd = '5', width=20, height=2)
btn1.grid(row=11, column=1, padx=20, pady=10)
win.geometry("1400x700+250+250")
win.mainloop()
Edit 2
Code changed to work with classes:
from tkinter import *
from tkinter import ttk
from PIL import ImageTk ,Image
class ImageCalculator:
def __init__(self, img_path):
self.window = Toplevel()
self.window.geometry("1400x700+250+250")
self.mainframe = Frame(self.window)
self.mainframe.pack(expand=True, fill=BOTH)
self.bg_color = 'crimson'
frame_settings = {'master': self.mainframe, 'bd': 4,
'relief': RIDGE, 'bg': self.bg_color}
frame_names = ('left', 'center', 'right')
self.frames = {name: Frame(**frame_settings) for name in frame_names}
frame_height = 625
init_y = 80
frame_widths = {'left': 465, 'center': 485, 'right': 465}
x = 0
for name in frame_names:
frame_width = frame_widths[name]
self.frames[name].place(x=x, y=init_y, width=frame_width,
height=frame_height)
x += frame_width
self.setup_right_wrapper()
self.code_btn = self.setup_left_wrapper()
self.loaded_image = ImageTk.PhotoImage(Image.open(img_path))
self.add_strvar = StringVar()
self.sub_strvar = StringVar()
self.pro_strvar = StringVar()
def setup_left_wrapper(self) -> Button:
code_btn = Button(self.frames['left'], text='OPEN CODE', command=self.code,
bd='5', width=20, height=2)
img_btn = Button(self.frames['left'], text='Image', bd='5', width=15,
height=2, command=self.add_img)
code_btn.grid(row=11, column=1, padx=20, pady=10)
img_btn.grid(row=12, column=1, padx=20, pady=10)
return code_btn
def setup_right_wrapper(self):
right_frame_title = Label(self.frames['right'], text="Selected Data",
bg=self.bg_color, fg="white",
font=("times new roman",30,"bold"))
right_frame_title.grid(row=0, column=0, padx=20, pady=10)
def code(self):
def Find():
self.add_strvar.set(float(first_entry.get())
+ float(second_entry.get()))
self.sub_strvar.set(float(first_entry.get())
- float(second_entry.get()))
self.pro_strvar.set(float(first_entry.get())
* float(second_entry.get()))
self.code_btn.destroy()
Label2 = Label(self.frames['center'], image=self.loaded_image)
Label2.grid(row=0, column=0, padx=10, pady=5, sticky='w')
left_frame = self.frames['left']
first_entry = Entry(left_frame, width=15)
second_entry = Entry(left_frame, width=15)
# Settings of all labels
lbl_settings = {'bg': self.bg_color, 'fg': 'white',
'font': ("times new roman", 15, "bold")}
# Setting of all entry.
entry_settings = {'width': 15, 'state': 'readonly'}
add_lbl = Label(left_frame, text="Add", **lbl_settings)
add_entry = Entry(left_frame, textvariable=self.add_strvar,
**entry_settings)
sub_lbl = Label(left_frame, text="Subtract", **lbl_settings)
sub_entry = Entry(left_frame, textvariable=self.sub_strvar,
**entry_settings)
pro_lbl = Label(left_frame, text="Product", **lbl_settings)
pro_entry = Entry(left_frame, textvariable=self.pro_strvar,
**entry_settings)
calc_btn = Button(left_frame, text='Calculate', command=Find, bd='5',
width=15, height=2)
# Widget placement.
first_entry.grid(row=4, column=1, padx=10, pady=10, sticky='w')
second_entry.grid(row=5, column=1, padx=10, pady=10, sticky='w')
add_lbl.grid(row=6, column=0, padx=20, pady=10, sticky='w')
add_entry.grid(row=6, column=1, padx=10, pady=10, sticky='w')
sub_lbl.grid(row=7, column=0, padx=20, pady=10, sticky='w')
sub_entry.grid(row=7, column=1, padx=10, pady=10, sticky='w')
pro_lbl.grid(row=8, column=0, padx=20, pady=10, sticky='w')
pro_entry.grid(row=8, column=1, padx=10, pady=10, sticky='w')
calc_btn.grid(row=11, column=1, padx=20, pady=10)
def add_img(self):
if self.add_strvar.get() == "4.0":
Label2 = Label(self.frames['right'], image=self.loaded_image)
Label2.grid(row=0, column=2, padx=10, pady=5, sticky='w')
print("Move ahead")
else:
print("Try again")
def main():
img_calc = ImageCalculator('Amritsar.jpg')
mainloop()
if __name__ == "__main__":
main()
After setting an image for a Label, you need to keep a reference to this image. Otherwise, it's removed at the end of the function and you lose the image (it's garbage collected).
So, when you define your label, just add a line that stores the image as an attribute of the label:
img=ImageTk.PhotoImage(Image.open("Amritsar.jpg"))
Label2= Label(wrapper2, image=img)
Label2.grid(row=0, column=0, padx=10, pady=5, sticky='w')
Label2.dontloseit = img
The Label2 widget now has a nonsensical attribute called .dontloseit which holds the image. Now, it won't get collected and it will show in your tkinter widget.
It's one of the peculiarities of tkinter.
I am mainly coding on my MAC.
I created a GUI that is used for a tool I am using to automate things with selenium.
I have used .grid for all widgets in my GUI.
But when I open the GUI on my Windows laptop at home (same resolution but much smaller monitor panel) it is totally messed up.
Here are two screenshot showing the problem,
Messed up layout on Win Laptop (17,3")
The second screenshot shows how it should look.
This is the code I used for the grid layout:
# General GUI settings
app = Tk()
app.title('trade_buddy')
app.geometry('605x800')
app.minsize(605, 800)
app.maxsize(605, 800)
app.grid_rowconfigure(0, weight=1)
app.grid_rowconfigure(1, weight=1)
app.grid_rowconfigure(2, weight=1)
app.grid_rowconfigure(3, weight=1)
app.grid_rowconfigure(4, weight=1)
app.grid_rowconfigure(5, weight=1)
app.grid_rowconfigure(6, weight=1)
app.grid_rowconfigure(7, weight=1)
app.grid_rowconfigure(8, weight=1)
app.grid_rowconfigure(9, weight=1)
app.grid_rowconfigure(10, weight=1)
app.grid_rowconfigure(11, weight=1)
app.grid_rowconfigure(12, weight=1)
app.grid_rowconfigure(13, weight=1)
#Background label
image = Image.open('v2.0/lolo.png')
image = image.resize((600,450), Image.ANTIALIAS)
copy_of_image = image.copy()
photo = ImageTk.PhotoImage(image)
label = tk.Label(app, image = photo)
label.place(x=0, y=0)
#Buttons
b_init = tk.Button(app,text='Initialize',font='Tahoma 10 bold',bg='#f8f09d',fg='#1e1e1f',activebackground='#1e1e1f',activeforeground='#f8f09d',padx=8, pady=10, command=lambda:threading.Thread(target=tb,daemon=True).start())
b_exit = tk.Button(app,text='Exit',font='Tahoma 10 bold',padx=8,bg='#f8f09d',fg='#1e1e1f',activebackground='#1e1e1f',activeforeground='#f8f09d', pady=10,command=lambda:threading.Thread(target=exit_tb,daemon=True).start())
b_start = tk.Button(app,text='Start',font='Tahoma 10 bold',bg='#f8f09d',fg='#1e1e1f',activebackground='#1e1e1f',activeforeground='#f8f09d',padx=8, pady=10,command=lambda:threading.Thread(target=filters,daemon=True).start())
b_pause = tk.Button(app,text='Pause',font='Tahoma 10 bold',bg='#f8f09d',fg='#1e1e1f',activebackground='#1e1e1f',activeforeground='#f8f09d',padx=8, pady=10,command=lambda:threading.Thread(target=stop_tb,daemon=True).start())
b_tfm = tk.Button(app,text='TFM',font='Tahoma 10 bold',bg='#f8f09d',fg='#1e1e1f',activebackground='#1e1e1f',activeforeground='#f8f09d',padx=8, pady=10,command=lambda:threading.Thread(target=tfm,daemon=True).start())
#Labels
l_maxBO = tk.Label(app,text='Maximum buy price:',bg='#1e1e1f', fg='#f8f09d',font='Tahoma 10')
l_itemsontf = tk.Label(text='# of items on transfer list:',bg='#1e1e1f', fg='#f8f09d',font='Tahoma 10')
l_speed = tk.Label(text='Choose speed:',bg='#1e1e1f', fg='#f8f09d',font='Tahoma 10 bold')
l_routine = tk.Label(text='Choose routine:',bg='#1e1e1f', fg='#f8f09d',font='Tahoma 10 bold')
#Entries
e_maxBO = tk.Entry(app, width=10, bg='#1e1e1f', fg='#f8f09d', font='Tahoma 10')
e_itemsontf = tk.Entry(app, width=10, bg='#1e1e1f', fg='#f8f09d',font='Tahoma 10')
e_fixedsellprice = tk.Entry(app, width=10, bg='#1e1e1f', fg='#f8f09d',font='Tahoma 10')
#Text box
t_outputbox = tk.Text(app, width=99, height=27, font='Tahoma 10',bg='#2c2c2c', fg='#f8f09d', relief=SUNKEN, highlightthickness="1")
#Grid Layout
l_maxBO.grid(row=0, column=0, sticky='w', padx=5, pady=5)
l_itemsontf.grid(row=1, column=0, sticky='w', padx=5, pady=5)
l_speed.grid(row=3, column=0, sticky='w', padx=5, pady=1, ipady=1)
l_routine.grid(row=7, column=0, sticky='w', padx=5, pady=1, ipady=1)
e_maxBO.grid(row=0, column=1, sticky='w', padx=5, pady=5)
e_itemsontf.grid(row=1, column=1, sticky='w', padx=5, pady=5)
e_fixedsellprice.grid(row=11, column=0, sticky='w', padx=5, pady=3)
b_exit.grid(row=12, column=8, sticky='sw', padx=5, pady=10)
b_init.grid(row=12, column=4, sticky='sw', padx=5, pady=10)
b_start.grid(row=12, column=0, sticky='sw', padx=5, pady=10)
b_pause.grid(row=12, column=1, sticky='sw', padx=5, pady=10)
b_tfm.grid(row=12, column=2, sticky='sw', padx=5, pady=10)
r_normal.grid(row=4, column=0, sticky='w', padx=5, pady=1)
r_fast.grid(row=5, column=0, sticky='w', padx=5, pady=1)
r_slow.grid(row=6, column=0, sticky='w', padx=5, pady=1)
r_buyonly.grid(row=8, column=0, sticky='w', padx=5, pady=1)
r_fullroutine.grid(row=9, column=0, sticky='w', padx=5, pady=1)
r_buysellfixed.grid(row=10, column=0, sticky='w', padx=5, pady=1)
t_outputbox.grid(row=13, column=0, columnspan=13, rowspan=13, sticky='nsew', padx=5, pady=10)
As a coding beginner, I really have no idea what else I could change.
I already changed from .place to .grid but the problem is still the same.
Does anyone have an idea how I could setup my GUI that it keeps the minimum required geometry relations no matter on what monitor I work?
Here is an example of forcing it to look like the second picture.
import tkinter as tk
from PIL import ImageTk, Image
# General GUI settings
app = tk.Tk()
app.configure(bg="black")
#Background label
image = Image.open('test.png')
image = image.resize((200,200), Image.ANTIALIAS)
copy_of_image = image.copy()
photo = ImageTk.PhotoImage(image)
label = tk.Label(app, image = photo)
label.grid(row=3, column=1, sticky='w')
#others
r_normal = tk.Radiobutton(app, text="Normal Speed")
r_fast = tk.Radiobutton(app, text="Fast Speed")
r_slow = tk.Radiobutton(app, text="Slow Speed")
r_buyonly = tk.Radiobutton(app, text="Buy only")
r_fullroutine = tk.Radiobutton(app, text="Full routine (optimized price)")
r_buysellfixed = tk.Radiobutton(app, text="Buy and sell for fixed price")
#Buttons
b_init = tk.Button(app,text='Initialize',font='Tahoma 10 bold',bg='#f8f09d',fg='#1e1e1f',activebackground='#1e1e1f',activeforeground='#f8f09d',padx=8, pady=10, command=lambda:threading.Thread(target=tb,daemon=True).start())
b_exit = tk.Button(app,text='Exit',font='Tahoma 10 bold',padx=8,bg='#f8f09d',fg='#1e1e1f',activebackground='#1e1e1f',activeforeground='#f8f09d', pady=10,command=lambda:threading.Thread(target=exit_tb,daemon=True).start())
b_start = tk.Button(app,text='Start',font='Tahoma 10 bold',bg='#f8f09d',fg='#1e1e1f',activebackground='#1e1e1f',activeforeground='#f8f09d',padx=8, pady=10,command=lambda:threading.Thread(target=filters,daemon=True).start())
b_pause = tk.Button(app,text='Pause',font='Tahoma 10 bold',bg='#f8f09d',fg='#1e1e1f',activebackground='#1e1e1f',activeforeground='#f8f09d',padx=8, pady=10,command=lambda:threading.Thread(target=stop_tb,daemon=True).start())
b_tfm = tk.Button(app,text='TFM',font='Tahoma 10 bold',bg='#f8f09d',fg='#1e1e1f',activebackground='#1e1e1f',activeforeground='#f8f09d',padx=8, pady=10,command=lambda:threading.Thread(target=tfm,daemon=True).start())
#Labels
l_maxBO = tk.Label(app,text='Maximum buy price:',bg='#1e1e1f', fg='#f8f09d',font='Tahoma 10')
l_itemsontf = tk.Label(text='# of items on transfer list:',bg='#1e1e1f', fg='#f8f09d',font='Tahoma 10')
l_speed = tk.Label(text='Choose speed:',bg='#1e1e1f', fg='#f8f09d',font='Tahoma 10 bold')
l_routine = tk.Label(text='Choose routine:',bg='#1e1e1f', fg='#f8f09d',font='Tahoma 10 bold')
#Entries
e_maxBO = tk.Entry(app, width=10, bg='#1e1e1f', fg='#f8f09d', font='Tahoma 10')
e_itemsontf = tk.Entry(app, width=10, bg='#1e1e1f', fg='#f8f09d',font='Tahoma 10')
e_fixedsellprice = tk.Entry(app, width=10, bg='#1e1e1f', fg='#f8f09d',font='Tahoma 10')
#Text box
t_outputbox = tk.Text(app, width=99, height=27, font='Tahoma 10',bg='#2c2c2c', fg='#f8f09d', relief=tk.SUNKEN, highlightthickness="1")
#Grid Layout
l_maxBO.grid(row=0, column=0, sticky='w', padx=5, pady=5)
l_itemsontf.grid(row=1, column=0, sticky='w', padx=5, pady=5)
l_speed.grid(row=3, column=0, sticky='w', padx=5, pady=1, ipady=1)
l_routine.grid(row=7, column=0, sticky='w', padx=5, pady=1, ipady=1)
e_maxBO.grid(row=0, column=1, sticky='w', padx=5, pady=5)
e_itemsontf.grid(row=1, column=1, sticky='w', padx=5, pady=5)
e_fixedsellprice.grid(row=11, column=0, sticky='w', padx=5, pady=3)
b_exit.grid(row=12, column=8, sticky='sw', padx=5, pady=10)
b_init.grid(row=12, column=4, sticky='sw', padx=5, pady=10)
b_start.grid(row=12, column=0, sticky='sw', padx=5, pady=10)
b_pause.grid(row=12, column=1, sticky='sw', padx=5, pady=10)
b_tfm.grid(row=12, column=2, sticky='sw', padx=5, pady=10)
r_normal.grid(row=4, column=0, sticky='w', padx=5, pady=1)
r_fast.grid(row=5, column=0, sticky='w', padx=5, pady=1)
r_slow.grid(row=6, column=0, sticky='w', padx=5, pady=1)
r_buyonly.grid(row=8, column=0, sticky='w', padx=5, pady=1)
r_fullroutine.grid(row=9, column=0, sticky='w', padx=5, pady=1)
r_buysellfixed.grid(row=10, column=0, sticky='w', padx=5, pady=1)
t_outputbox.grid(row=13, column=0, columnspan=13, rowspan=13, sticky='nsew', padx=5, pady=10)
Notable differences:
I had to set a background color to make up for how we are moving that image.
The image is no longer using .place and instead using .grid.
The image will need to be resized a bit differently because it is a smaller area.
I removed all those row_configures because they were removing some rigidity, you can add it back if you like, but you'll have to re-scale your columns to match the new layout.
Let us know if you have any questions etc.
I try to build a student management system while this tkinter window does not show after running following code. Can someone give some advice here? This is public source code from youtube chanel.
from tkinter import *
from tkinter import ttk
class Student:
def __init__(self,root):
self.root=root
self.root.title("student management system")
self.root.geometry("1350*700+0+0")
title=Label(self.root,text="Student Management System",bd=10,relief=GROOVE,front=("time new roman",40,"bold"),bg="yellow",fg="red")
title.pack(side=TOP,fill=X)
Manage_Frame=Frame(self.root,bd=4,relief=RIDGE,bg="crimson")
Manage_Frame.place(x=20,y=70,width=450,height=560)
#========Massage Frame===============================
Manage_Frame=Frame(self.root,bd=4,relief=RIDGE,bg="crimson")
Manage_Frame.place(x=20,y=100,width=450,height=560)
m_title=Label(Manage_Frame,text="Manage Students",bg="crimson",fg="white",front=("time new roman",30,"bold"))
m_title.grid(row=0,columnspan=2,pady=10)
lbl_roll = Label(Manage_Frame, text="Roll No.", bg="crimson", fg="white",front=("time new roman", 20, "bold"))
lbl_roll.grid(row=1, colum=0, pady=10,padx=20,sticky="w")
txt_roll = Entry(Manage_Frame,front=("time new roman", 15, "bold"),bd=5,relief=GROOVE)
txt_roll.grid(row=1, colum=2, pady=10, padx=20, sticky="w")
lbl_name = Label(Manage_Frame, text="Name", bg="crimson", fg="white", front=("time new roman", 20, "bold"))
lbl_name.grid(row=1, colum=0, pady=10, padx=20, sticky="w")
txt_name = Entry(Manage_Frame, front=("time new roman", 15, "bold"), bd=5, relief=GROOVE)
txt_name.grid(row=2, colum=1, pady=10, padx=20, sticky="w")
lbl_Email = Label(Manage_Frame, text="Email", bg="crimson", fg="white", front=("time new roman", 20, "bold"))
lbl_Email.grid(row=1, colum=0, pady=10, padx=20, sticky="w")
txt_Email = Entry(Manage_Frame, front=("time new roman", 15, "bold"), bd=5, relief=GROOVE)
txt_Email.grid(row=3, colum=0, pady=10, padx=20, sticky="w")
lbl_Gender = Label(Manage_Frame, text="Gender", bg="crimson", fg="white", front=("time new roman", 20, "bold"))
lbl_Gender.grid(row=4, colum=0, pady=10, padx=20, sticky="w")
combo_Gender=ttk.Combobox(Manage_Frame,front=("times new roman",20,"bold"))
combo_Gender['values']=("Male","Female","other")
combo_Gender.grid(row=4,colum=1,padx=20,pady=10)
lbl_Contact = Label(Manage_Frame, text="Contact", bg="crimson", fg="white", front=("time new roman", 20, "bold"))
lbl_Contact.grid(row=5, colum=0, pady=10, padx=20, sticky="w")
txt_Contact = Entry(Manage_Frame, front=("time new roman", 15, "bold"), bd=5, relief=GROOVE)
txt_Contact.grid(row=5, colum=1, pady=10, padx=20, sticky="w")
lbl_DOB = Label(Manage_Frame, text="DOB", bg="crimson", fg="white",front=("time new roman", 20, "bold"))
lbl_DOB.grid(row=6, colum=0, pady=10, padx=20, sticky="w")
txt_Contact = Entry(Manage_Frame, front=("time new roman", 15, "bold"), bd=5, relief=GROOVE)
txt_Contact.grid(row=6, colum=1, pady=10, padx=20, sticky="w")
lbl_address = Label(Manage_Frame, text="Address", bg="crimson", fg="white", front=("time new roman", 20, "bold"))
lbl_address.grid(row=6, colum=0, pady=10, padx=20, sticky="w")
txt_address = Entry(Manage_Frame, front=("time new roman", 15, "bold"), bd=5, relief=GROOVE)
txt_address.grid(row=6, colum=1, pady=10, padx=20, sticky="w")
#=========Button Frame=========
btn_Frame=Frame(Manage_Frame,bd=4,relief=RIDGE,bg="crimson")
btn_Frame.place(x=15, y=500, width=420)
Addbtn = Button(btn_Frame,text="add",width=10).grid(row=0,column=0,padx=10,pady=10)
Updatebtn = Button(btn_Frame, text="update", width=10).grid(row=0, column=1, padx=10, pady=10)
Deletebtn = Button(btn_Frame, text="delete", width=10).grid(row=0, column=2, padx=10, pady=10)
Clearbtn = Button(btn_Frame, text="clear", width=10).grid(row=0, column=3, padx=10, pady=10)
#=========Detail Frame=========
Detail_Frame = Frame(self.root, bd=4, relief=RIDGE, bg="crimson")
Detail_Frame.place(x=500, y=100, width=800, height=580)
lbl_search = Label(Detail_Frame, text="Search By", bg="crimson", fg="white", front=("time new roman", 20, "bold"))
lbl_search.grid(row=0, colum=0, pady=10, padx=20, sticky="w")
combo_search = ttk.Combobox(Manage_Frame,width=10,front=("times new roman", 13, "bold"),state="readonly")
combo_search['values'] = ("Roll", "Name", "contact")
combo_search.grid(row=0, colum=1, padx=20, pady=10)
txt_Search = Entry(Manage_Frame,width=15, front=("time new roman", 15, "bold"), bd=5, relief=GROOVE)
txt_Search.grid(row=6, colum=1, pady=10, padx=20, sticky="w")
searchbtn = Button(btn_Frame, text="Search", width=10).grid(row=0, column=3, padx=10, pady=10)
showallbtn = Button(btn_Frame, text="Show All", width=10).grid(row=0, column=4, padx=10, pady=10)
#=========Table Frame=========
Table_Frame = Frame(Detail_Frame, bd=4, relief=RIDGE, bg="crimson")
Table_Frame.place(x=10, y=70, width=760, height=500)
scroll_x=Scrollbar(Table_Frame,orient=HORIZONTAL)
scroll_y = Scrollbar(Table_Frame, orient=VERTICAL)
Student_table=ttk.Treeview(Table_Frame,columns=("roll","name","email","gender","contact","dob","Address"),xscollcommand=scroll_x.set,yscollcommand=scroll_y.set)
scroll_x.pack(side=BOTTOM,fill=X)
scroll_y.pack(side=RIGHT, fill=Y)
scroll_x.config(command=Student_table.xview)
scroll_y.config(command=Student_table.xview)
Student_table.heading("roll",text="Roll")
Student_table.heading("name", text="Name")
Student_table.heading("email", text="Email")
Student_table.heading("gender", text="Gender")
Student_table.heading("Contact", text="Contact")
Student_table.heading("D.O.B", text="D.O.B")
Student_table.heading("Address", text="Address")
Student_table['show']='headings'
Student_table.pack()
root=Tk()
You have to initialize your Student object and pass root to it. Add this to the end of your code for it to run:
app=Student(root)
root.mainloop()
Your current code as is includes a lot of syntax errors that need to be fixed before running. The following should run, albeit with some visual errors:
from tkinter import *
from tkinter import ttk
class Student:
def __init__(self,root):
self.root=root
self.root.title("student management system")
self.root.geometry("1350x700")
title=Label(self.root,text="Student Management System",bd=10,relief=GROOVE,font=("time new roman",40,"bold"),bg="yellow",fg="red")
title.pack(side=TOP,fill=X)
Manage_Frame=Frame(self.root,bd=4,relief=RIDGE,bg="crimson")
Manage_Frame.place(x=20,y=70,width=450,height=560)
#========Massage Frame===============================
Manage_Frame=Frame(self.root,bd=4,relief=RIDGE,bg="crimson")
Manage_Frame.place(x=20,y=100,width=450,height=560)
m_title=Label(Manage_Frame,text="Manage Students",bg="crimson",fg="white",font=("time new roman",30,"bold"))
m_title.grid(row=0,columnspan=2,pady=10)
lbl_roll = Label(Manage_Frame, text="Roll No.", bg="crimson", fg="white",font=("time new roman", 20, "bold"))
lbl_roll.grid(row=1, column=0, pady=10,padx=20,sticky="w")
txt_roll = Entry(Manage_Frame,font=("time new roman", 15, "bold"),bd=5,relief=GROOVE)
txt_roll.grid(row=1, column=2, pady=10, padx=20, sticky="w")
lbl_name = Label(Manage_Frame, text="Name", bg="crimson", fg="white", font=("time new roman", 20, "bold"))
lbl_name.grid(row=1, column=0, pady=10, padx=20, sticky="w")
txt_name = Entry(Manage_Frame, font=("time new roman", 15, "bold"), bd=5, relief=GROOVE)
txt_name.grid(row=2, column=1, pady=10, padx=20, sticky="w")
lbl_Email = Label(Manage_Frame, text="Email", bg="crimson", fg="white", font=("time new roman", 20, "bold"))
lbl_Email.grid(row=1, column=0, pady=10, padx=20, sticky="w")
txt_Email = Entry(Manage_Frame, font=("time new roman", 15, "bold"), bd=5, relief=GROOVE)
txt_Email.grid(row=3, column=0, pady=10, padx=20, sticky="w")
lbl_Gender = Label(Manage_Frame, text="Gender", bg="crimson", fg="white", font=("time new roman", 20, "bold"))
lbl_Gender.grid(row=4, column=0, pady=10, padx=20, sticky="w")
combo_Gender=ttk.Combobox(Manage_Frame,font=("times new roman",20,"bold"))
combo_Gender['values']=("Male","Female","other")
combo_Gender.grid(row=4,column=1,padx=20,pady=10)
lbl_Contact = Label(Manage_Frame, text="Contact", bg="crimson", fg="white", font=("time new roman", 20, "bold"))
lbl_Contact.grid(row=5, column=0, pady=10, padx=20, sticky="w")
txt_Contact = Entry(Manage_Frame, font=("time new roman", 15, "bold"), bd=5, relief=GROOVE)
txt_Contact.grid(row=5, column=1, pady=10, padx=20, sticky="w")
lbl_DOB = Label(Manage_Frame, text="DOB", bg="crimson", fg="white",font=("time new roman", 20, "bold"))
lbl_DOB.grid(row=6, column=0, pady=10, padx=20, sticky="w")
txt_Contact = Entry(Manage_Frame, font=("time new roman", 15, "bold"), bd=5, relief=GROOVE)
txt_Contact.grid(row=6, column=1, pady=10, padx=20, sticky="w")
lbl_address = Label(Manage_Frame, text="Address", bg="crimson", fg="white", font=("time new roman", 20, "bold"))
lbl_address.grid(row=6, column=0, pady=10, padx=20, sticky="w")
txt_address = Entry(Manage_Frame, font=("time new roman", 15, "bold"), bd=5, relief=GROOVE)
txt_address.grid(row=6, column=1, pady=10, padx=20, sticky="w")
#=========Button Frame=========
btn_Frame=Frame(Manage_Frame,bd=4,relief=RIDGE,bg="crimson")
btn_Frame.place(x=15, y=500, width=420)
Addbtn = Button(btn_Frame,text="add",width=10).grid(row=0,column=0,padx=10,pady=10)
Updatebtn = Button(btn_Frame, text="update", width=10).grid(row=0, column=1, padx=10, pady=10)
Deletebtn = Button(btn_Frame, text="delete", width=10).grid(row=0, column=2, padx=10, pady=10)
Clearbtn = Button(btn_Frame, text="clear", width=10).grid(row=0, column=3, padx=10, pady=10)
#=========Detail Frame=========
Detail_Frame = Frame(self.root, bd=4, relief=RIDGE, bg="crimson")
Detail_Frame.place(x=500, y=100, width=800, height=580)
lbl_search = Label(Detail_Frame, text="Search By", bg="crimson", fg="white", font=("time new roman", 20, "bold"))
lbl_search.grid(row=0, column=0, pady=10, padx=20, sticky="w")
combo_search = ttk.Combobox(Manage_Frame,width=10,font=("times new roman", 13, "bold"),state="readonly")
combo_search['values'] = ("Roll", "Name", "contact")
combo_search.grid(row=0, column=1, padx=20, pady=10)
txt_Search = Entry(Manage_Frame,width=15, font=("time new roman", 15, "bold"), bd=5, relief=GROOVE)
txt_Search.grid(row=6, column=1, pady=10, padx=20, sticky="w")
searchbtn = Button(btn_Frame, text="Search", width=10).grid(row=0, column=3, padx=10, pady=10)
showallbtn = Button(btn_Frame, text="Show All", width=10).grid(row=0, column=4, padx=10, pady=10)
#=========Table Frame=========
Table_Frame = Frame(Detail_Frame, bd=4, relief=RIDGE, bg="crimson")
Table_Frame.place(x=10, y=70, width=760, height=500)
scroll_x=Scrollbar(Table_Frame,orient=HORIZONTAL)
scroll_y = Scrollbar(Table_Frame, orient=VERTICAL)
Student_table=ttk.Treeview(Table_Frame,columns=("roll","name","email","gender","contact","dob","Address"),xscrollcommand=scroll_x.set,yscrollcommand=scroll_y.set)
scroll_x.pack(side=BOTTOM,fill=X)
scroll_y.pack(side=RIGHT, fill=Y)
scroll_x.config(command=Student_table.xview)
scroll_y.config(command=Student_table.xview)
Student_table.heading("roll",text="Roll")
Student_table.heading("name", text="Name")
Student_table.heading("email", text="Email")
Student_table.heading("gender", text="Gender")
Student_table.heading("contact", text="Contact")
Student_table.heading("dob", text="D.O.B")
Student_table.heading("Address", text="Address")
Student_table['show']='headings'
Student_table.pack()
root=Tk()
app=Student(root)
root.mainloop()
I have a grid placing a top, center, and footer frames, then I'm making frames inside the Center frame (I'll call them subframes as I don't know what to refer to them as). When I try to pack/grid/place a button inside the subframes it won't work. If I grid the buttons, they appear at the bottom of all top-level frames which doesn't make any sense to me since I put them in their appropriate subframes... This is really hard to explain, but that's the best I can do.
from tkinter import messagebox
import math
# gui
root = tk.Tk()
root.title('Bid Generator')
root.geometry('{}x{}'.format(800, 880))
# main containers
top_frame = tk.Frame(root, bg='#00c3ff', height=75)
center = tk.Frame(root, bg='white')
footer = tk.Frame(root, bg='black', height=100)
# layout of the main containers
root.grid_rowconfigure(1, weight=1)
root.grid_columnconfigure(0, weight=1)
top_frame.grid(row=0, sticky="ew")
center.grid(row=1, sticky="nsew")
footer.grid(row=2, sticky="ew")
# distributing equal weight (priority) to center frame columns
center.grid_columnconfigure(0, weight=1)
center.grid_columnconfigure(1, weight=1)
center.grid_columnconfigure(2, weight=1)
center.grid_rowconfigure(0, weight=1)
center.grid_rowconfigure(1, weight=1)
center.grid_rowconfigure(2, weight=1)
center.grid_rowconfigure(3, weight=1)
center.grid_rowconfigure(4, weight=1)
center.grid_rowconfigure(5, weight=1)
center.grid_rowconfigure(6, weight=1)
center.grid_rowconfigure(7, weight=1)
center.grid_rowconfigure(8, weight=1)
center.grid_rowconfigure(9, weight=1)
center.grid_rowconfigure(10, weight=1)
center.grid_rowconfigure(11, weight=1)
center.grid_rowconfigure(12, weight=1)
# label widget for the top frame
aibs_label = tk.Label(top_frame, text='Bid Generator', bg='#00c3ff').pack()
# Supported bank frames
bank_label = tk.Label(center, text='Supported Bank?', bg="gray", height=20).grid(row=0, columnspan=3, sticky="ew")
bank_left = tk.Frame(center, bg="red", padx=10, pady=10, height=10, width=10).grid(row=1, column=0, sticky="ew")
bank_mid = tk.Frame(center, bg="green", padx=10, pady=10, height=10, width=10).grid(row=1, column=1, sticky="ew")
bank_right = tk.Frame(center, bg="blue", padx=10, pady=10, height=10, width=10).grid(row=1, column=2, sticky="ew")
bank_yes = tk.Button(bank_left, text="Yes", width=24, height=3).grid()
bank_no = tk.Button(bank_mid, text="No", width=24, height=3).grid()
bank_name = tk.Button(bank_right, text="entry", width=24, height=3).grid()
# credit cards
cc_label = tk.Label(center, text='Credit cards?', bg="gray", height=20).grid(row=1, columnspan=3, sticky="ew")
cc_left = tk.Frame(center, bg="red", height=10, width=10).grid(row=2, column=0, sticky="ew")
cc_mid = tk.Frame(center, bg="green", height=10, width=10).grid(row=2, column=1, sticky="ew")
cc_right = tk.Frame(center, bg="blue", height=10, width=10).grid(row=2, column=2, sticky="ew")
# checks frames
chks_label = tk.Label(center, text='Checks?', bg="gray", height=20).grid(row=3, columnspan=3, sticky="ew")
chks_left = tk.Frame(center, bg="red", height=10, width=10).grid(row=4, column=0, sticky="ew")
chks_mid = tk.Frame(center, bg="green", height=10, width=10).grid(row=4, column=1, sticky="ew")
chks_right = tk.Frame(center, bg="blue", height=10, width=10).grid(row=4, column=2, sticky="ew")
# payroll frames
pr_label = tk.Label(center, text='Payroll?', bg="gray", height=20).grid(row=5, columnspan=3, sticky="ew")
pr_left = tk.Frame(center, bg="red", height=10, width=10).grid(row=6, column=0, sticky="ew")
pr_mid = tk.Frame(center, bg="green", height=10, width=10).grid(row=6, column=1, sticky="ew")
pr_right = tk.Frame(center, bg="blue", height=10, width=10).grid(row=6, column=2, sticky="ew")
# sales tax frames
st_label = tk.Label(center, text='Sales Tax?', bg="gray", height=20).grid(row=7, columnspan=3, sticky="ew")
st_left = tk.Frame(center, bg="red", height=10, width=10).grid(row=8, column=0, sticky="ew")
st_mid = tk.Frame(center, bg="green", height=10, width=10).grid(row=8, column=1, sticky="ew")
st_right = tk.Frame(center, bg="blue", height=10, width=10).grid(row=8, column=2, sticky="ew")
# workers comp frames
wc_label = tk.Label(center, text='Workers Comp?', bg="gray", height=20).grid(row=9, columnspan=3, sticky="ew")
wc_left = tk.Frame(center, bg="red", height=10, width=10).grid(row=10, column=0, sticky="ew")
wc_mid = tk.Frame(center, bg="green", height=10, width=10).grid(row=10, column=1, sticky="ew")
wc_right = tk.Frame(center, bg="blue", height=10, width=10).grid(row=10, column=2, sticky="ew")
# qbox frames
qbox_label = tk.Label(center, text='Qbox?', bg="gray", height=20).grid(row=11, columnspan=3, sticky="ew")
qbox_left = tk.Frame(center, bg="red", height=100, width=10).grid(row=12, column=0, sticky="ew")
qbox_mid = tk.Frame(center, bg="green", height=10, width=10).grid(row=12, column=1, sticky="ew")
qbox_right = tk.Frame(center, bg="blue", height=10, width=10).grid(row=12, column=2, sticky="ew")
# End of line
root.mainloop()
hi i am trying to resize images together with buttons on a grid whenever program window is resized,most of the guides just teach how to resize buttons but buttons or text inside of them stays the same .i think that this is possible with pack() but not sure and would like to know if its possible with grid.
thanks for your time.
from tkinter import *
import math
# ----- calculator class
class calc:
def getandreplace(self):
# replaces x with * and ÷ with /
self.expression = self.e.get()
self.newtext=self.expression.replace("/","/")
self.newtext=self.newtext.replace("x","*")
def equals(self):
# when = button is pressed
self.getandreplace()
try:
# evaluates expresion with eval command
self.value = eval(self.newtext)
except SyntaxError or NameError or ZeroDivisionError:
self.e.delete(0,END)
self.e.insert(0,"Invalid input!")
else:
self.e.delete(0,END)
self.e.insert(0,self.value)
def squareroot(self):
# square root
self.getandreplace()
try:
# evaluates expresion with eval command
self.value = eval(self.newtext)
except SyntaxError or NameError:
self.e.delete(0, END)
self.e.insert(0, "Invalid input!")
else:
self.sqrtval=math.sqrt(self.value)
self.e.delete(0,END)
self.e.insert(0,self.sqrtval)
def square(self):
# square
self.getandreplace()
try:
# evaluates expresion with eval command
self.value = eval(self.newtext)
except SyntaxError or NameError:
self.e.delete(0, END)
self.e.insert(0, "Invalid input!")
else:
self.sqval=math.pow(self.value,2)
self.e.delete(0,END)
self.e.insert(0,self.sqval)
def clearall(self):
# function to clear input from screen
self.e.delete(0,END)
def clear1(self):
self.txt=self.e.get()[:-1]
self.e.delete(0,END)
self.e.insert(0,self.txt)
def action(self,argi):
# when presing button,value typed will be inserted into end of text area
self.e.insert(END,argi)
def __init__(self,master):
# constructor
for x in range(6):
for y in range(4):
Grid.columnconfigure(master, x, weight=1)
Grid.rowconfigure(master, y, weight=1)
master.title("Calculator")
master.geometry('500x500')
master.iconbitmap(r'number_5.ico')
master.minsize(300,370)
self.e = Entry(master,font=20)
self.e.grid(row=0,column=0,columnspan=6,pady=16, sticky=N+S+E+W)
self.e.focus_set() # sets focus to input text area
# makes buttons
Button(master,text="=",width=11,height=3,font=("Helvetica", 15),fg="black",
bg="white",command=lambda:self.equals()).grid(
row=4, column=4,columnspan=2, sticky=N+S+E+W)
Button(master, text="cl_all",width=5,height=3,font=("Helvetica", 15),
fg="red", bg = "light green",
command=lambda:self.clearall()).grid(row=1, column=4, sticky=N+S+E+W)
Button(master, text="cl_one", width=5, height=3,font=("Helvetica", 15),
fg="red", bg="light green",
command=lambda: self.clear1()).grid(row=1, column=5, sticky=N+S+E+W)
Button(master, text="+", width=5, height=3,font=("Helvetica", 15),
fg="black", bg="white",
command=lambda: self.action("+")).grid(row=4, column=3, sticky=N+S+E+W)
Button(master, text="x", width=5, height=3,font=("Helvetica", 15),
fg="black", bg="white",
command=lambda: self.action("x")).grid(row=2, column=3, sticky=N+S+E+W)
Button(master, text="-", width=5, height=3,font=("Helvetica", 15),
fg="black", bg="white",
command=lambda: self.action("-")).grid(row=3, column=3, sticky=N+S+E+W)
Button(master, text="÷", width=5, height=3,font=("Helvetica", 15),
fg="black", bg="white",
command=lambda: self.action("/")).grid(row=1, column=3, sticky=N+S+E+W)
Button(master, text="%", width=5, height=3,font=("Helvetica", 15),
fg="black", bg="white",
command=lambda: self.action("%")).grid(row=4, column=2, sticky=N+S+E+W)
Button(master, text="7", width=5, height=3,font=("Helvetica", 15),
fg="black", bg="light blue",
command=lambda: self.action("7")).grid(row=1, column=0, sticky=N+S+E+W)
Button(master, text="8", width=5, height=3,font=("Helvetica", 15),
fg="black", bg="light blue",
command=lambda: self.action("8")).grid(row=1, column=1, sticky=N+S+E+W)
Button(master, text="9", width=5, height=3,font=("Helvetica", 15),
fg="black", bg="light blue",
command=lambda: self.action("9")).grid(row=1, column=2, sticky=N+S+E+W)
Button(master, text="4", width=5, height=3,font=("Helvetica", 15),
fg="black", bg="light blue",
command=lambda: self.action("4")).grid(row=2, column=0, sticky=N+S+E+W)
photo = PhotoImage(file='number_5.png')
label_photo = Label(master, image=photo)
label_photo.photo = photo
label_photo.grid(column=1, row=2, sticky=N+S+E+W)
Button(master, image=photo, width=5, height=3,font=("Helvetica", 15),
fg="black", bg="light blue",
command=lambda: self.action("5")).grid( pady=2, padx=2, row=2, column=1, sticky=N+S+E+W)
Button(master, text="6", width=5, height=3,font=("Helvetica", 15),
fg="black", bg="light blue",
command=lambda: self.action("6")).grid(row=2, column=2, sticky=N+S+E+W)
Button(master, text="1", width=5, height=3,font=("Helvetica", 15),
fg="black", bg="light blue",
command=lambda: self.action("1")).grid(row=3, column=0, sticky=N+S+E+W)
Button(master, text="2", width=5, height=3,font=("Helvetica", 15),
fg="black", bg="light blue",
command=lambda: self.action("2")).grid(row=3, column=1, sticky=N+S+E+W)
Button(master, text="3", width=5, height=3,font=("Helvetica", 15),
fg="black", bg="light blue",
command=lambda: self.action("3")).grid(row=3, column=2, sticky=N+S+E+W)
Button(master, text="0", width=5, height=3,font=("Helvetica", 15),
fg="black", bg="light blue",
command=lambda: self.action(0)).grid(row=4, column=0, sticky=N+S+E+W)
Button(master, text=".", width=5, height=3,font=("Helvetica", 15),
fg="black", bg="azure3",
command=lambda: self.action(".")).grid(row=4, column=1, sticky=N+S+E+W)
Button(master, text="(", width=5, height=3,font=("Helvetica", 15),
fg="black", bg="azure3",
command=lambda: self.action("(")).grid(row=2, column=4, sticky=N+S+E+W)
Button(master, text=")", width=5, height=3,font=("Helvetica", 15),
fg="black", bg="azure3",
command=lambda: self.action(")")).grid(row=2, column=5, sticky=N+S+E+W)
Button(master, text="S_root", width=5, height=3,font=("Helvetica", 15),
fg="black", bg="azure3",
command=lambda: self.squareroot()).grid(row=3, column=4, sticky=N+S+E+W)
Button(master, text="x²", width=5, height=3,font=("Helvetica", 15),
fg="black", bg="azure3",
command=lambda: self.square()).grid(row=3, column=5, sticky=N+S+E+W)
window = Tk()
obj = calc(window)
window.mainloop()