I CAN'T CENTER 2 FRAMES WITH TKINTER - python

I am writing again because I have enough problems with grid to center 2 frames. before with .pack it was easier for me but now with grid I can't center my 2 frames in my window, what am I doing wrong??
self.wind = window
self.wind.title('Aplicación')
screen_width=self.wind.winfo_screenwidth()
screen_height=self.wind.winfo_screenheight()
x= (screen_width / 2) - (Product.WIDTH /2)
y= (screen_height / 2) - (Product.HEIGHT /2)
self.wind.geometry(f"{Product.WIDTH}x{Product.HEIGHT}+{int(x)}+{int(y)}")
self.wind.resizable(False, False)
frame2 = customtkinter.CTkFrame(self.wind, corner_radius=5)
#frame2.pack(expand="true")
frame2.grid(row=0, sticky="NSEW",pady=(20,20))
self.label_hora = Label(frame2, font = ('calibri', 25, 'bold'),background = 'purple', foreground = 'white')
#self.label_hora.pack(fill=BOTH, expand=1)
self.label_hora.grid(stick="NSEW")
self.tick()
#frame principal
frame = customtkinter.CTkFrame(self.wind, corner_radius=5)
#frame.pack(expand="true")
frame.grid(row=1)
button_llegue = customtkinter.CTkButton(master=frame, corner_radius=8,fg_color="IndianRed3",hover_color="IndianRed4", text='Llegue',command=self.add_fecha_llegue)
button_llegue.grid(row = 0, column = 0,pady=(20,10),padx=(15,15))
button_sali = customtkinter.CTkButton(master=frame, corner_radius=8,fg_color="PaleGreen3",hover_color="PaleGreen4", text='Me Voy',command=self.add_fecha_sali)
button_sali.grid(row = 0, column = 1,pady=(20,10),padx=(15,15))
self.tree = ttk.Treeview(master=frame,height = 10, selectmode="browse")
self.tree.grid(row = 1,column=0,pady=(10,20),columnspan=2,sticky="NWSE",padx=(15,15))
self.tree.heading('#0', text = 'Ultimos registros', anchor = CENTER)
scrollbar = ttk.Scrollbar(frame,orient='vertical',command=self.tree.yview)
scrollbar.grid(row = 1, column=0, sticky="NSE",pady=(10,20),columnspan=2,padx=(0,15))
self.tree.config(yscrollcommand=scrollbar.set)
self.get_fechas()

Related

can you insert a canvas inside a frame in tkinter? [duplicate]

This question already has answers here:
Why does Tkinter image not show up if created in a function?
(5 answers)
Closed 6 months ago.
I'm trying to display a frame with a canvas inside in order I'll be able to change of frame with a button, but when the code is executed it does not even display the button, I don't know if it happens because of all is on a function, if doing what I think is even possible or if I'm missing something
from tkinter import *
window = Tk()
window.geometry("1200x700")
window.configure(bg = "#ffffff")
def btn_clicked():
print("Button Clicked")
frame1 =Frame(window, width=1200, height=700)
frame1.grid(row=0, column=0)
def load_page():
frame1.tkraise()
frame1.pack_propagate(False)
canvas = Canvas(
frame1,
bg = "#263ff8",
height = 700,
width = 1200,
bd = 0,
highlightthickness = 0,
relief = "ridge")
canvas.place(x = 0, y = 0)
background_img = PhotoImage(file = f"background.png")
background = canvas.create_image(
601.0, 341.0,
image=background_img)
img0 = PhotoImage(file = f"img0.png")
boton = Button(
image = img0,
borderwidth = 0,
highlightthickness = 0,
command = btn_clicked,
relief = "flat")
boton.place(
x = 85, y = 71,
width = 430,
height = 99)
load_page()
window.resizable(False, False)
window.mainloop()
Rephrased the entire code and including image:
To see button display. I had to reduced height and width in Canvas. Also The button had to reduced. In line 21, I changed bd = 10. You can comment in , because I don't used PhotoImage. you can debug see in image.
from tkinter import *
window = Tk()
window.geometry("1200x700")
window.configure(bg = "#ffffff")
def btn_clicked():
print("Button Clicked")
frame1 =Frame(window, width=1200, height=700)
frame1.grid(row=0, column=0)
def load_page():
frame1.tkraise()
frame1.pack_propagate(False)
canvas = Canvas(
frame1,
bg = "#263ff8",
height = 100,
width = 200,
bd = 10,
highlightthickness = 0,
relief = "ridge")
canvas.place(x = 0, y = 0)
#background_img = PhotoImage(file = f"background.png")
#background = canvas.create_image(
#601.0, 341.0,
#image=background_img)
#img0 = PhotoImage(file = f"img0.png")
boton = Button(frame1,
#image = img0,
borderwidth = 0,
highlightthickness = 0,
command = btn_clicked,
relief = "flat")
boton.place(
x = 85, y = 71,
width = 30,
height =29)
load_page()
window.resizable(False, False)
window.mainloop()
Output result. you can see debug.

(Tkinter) Fill the gap between two background images, label border

I made a GUI for a Calendar. The blue background image has a grey border where the next image follows it.
I set the background image as Labels. So it seems like a label border.
The top and the Bottom of the image has a grey border. I suppose the grey border isn't on the sides because the image’s full width isn't accommodated to the application.
A picture here,
https://i.stack.imgur.com/73niQ.jpg
How do I get rid of the grey border where the two images connect?
Here’s the code,
import tkinter as tk
from tkinter import ttk
from tkinter import *
import calendar
from PIL import ImageTk, Image
screen = tk.Tk()
screen.geometry("1430x817")
title = screen.title("Class Calendar")
# Icon
img = tk.Image('photo', file = 'Calender_logo.png')
screen.tk.call('wm', 'iconphoto', screen._w, img)
# Scoll Bar
# main frame that holds everything.
main_frame = Frame(screen, bg = 'blue')
main_frame.pack(fill = BOTH, expand = 1)
# canvas that's getting scrolled
Canvasheight, Canvaswidth = 801, 1409.38
canvas = Canvas(main_frame, height = Canvasheight, width = Canvaswidth, borderwidth=0, highlightthickness =0, relief = 'flat', highlightcolor="white", highlightbackground="white")
canvas.pack(side = LEFT, fill= BOTH, expand = 1)
# Add a scrollbar to canvas
scroll_bar = ttk.Scrollbar( main_frame, orient=VERTICAL, command = canvas.yview)
scroll_bar.pack(side = RIGHT, fill= Y)
# Bind to mouse scrolling, configuring
canvas.configure(yscrollcommand=scroll_bar.set)
canvas.bind('<Configure>', lambda e: canvas.configure(scrollregion=canvas.bbox("all")))
# Another frame inside the canvas # To hold the things on the canvas
second_frame = Frame(main_frame, height = Canvasheight*8, width = Canvaswidth, borderwidth=0, highlightthickness =0, relief= 'flat', highlightcolor="white", highlightbackground="white")
# Add new frame to a window in the canvas
canvas.create_window((0,0), window = second_frame, anchor='nw', width= Canvaswidth) # 'w' near middle lower half in scroll bar
second_frame.pack_propagate(False)
####################################### THE BACKGROUND IMAGES
img1 = ImageTk.PhotoImage(Image.open("Screenshot 2022-06-28 at 10.29.24 AM.png"))
label1 = Label(second_frame, image = img1, borderwidth=0, highlightthickness =0, relief= 'flat', highlightcolor="white", highlightbackground="white")
label1.pack()
img2 = ImageTk.PhotoImage(Image.open("Screenshot 2022-06-28 at 10.29.24 AM.png"))
label2 = Label(second_frame, image = img2, borderwidth=0, highlightthickness =0, relief= 'flat', highlightcolor="white", highlightbackground="white")
label2.pack()
# MARCH
# PRINTING MARCH TO WINDOW
march22_label = Label(second_frame, text = "MARCH", font = ("arial", 27),fg='black', borderwidth=0, highlightthickness =0)
march22_label.config(bg = '#9ad0f0')
march22_label.place( x= 660, y = 18)
# PRINTING WEEK TO WINDOW
marchweek22_label = Label(second_frame, text =
"""
Monday Tuesday Wednesday Thursday Friday Saturday Sunday
""",
font= ('arial', 26),fg='black', borderwidth=0, highlightthickness =0)
marchweek22_label.config(bg = '#a1d0ea')
marchweek22_label.place(x= 100, y = 82)
def March22():
i = 1
x, y = 310, 194
while i < 7:
Button(second_frame, text = i, font = ("arial", 25), borderwidth=0, highlightthickness =0).place(x = x, y = y)
x = x + 185
i = i + 1
x, y = 125, y + 130
while i < 14:
Button(second_frame, text = i, font = ("arial", 25), borderwidth=0, highlightthickness =0).place(x = x, y = y)
x = x + 185
i = i + 1
x, y = 125, y + 130
while i < 21:
Button(second_frame, text = i, font = ("arial", 25),borderwidth=0, highlightthickness =0).place(x = x, y = y)
x = x + 185
i = i + 1
x, y = 125, y + 130
while i < 28:
Button(second_frame, text = i, font = ("arial", 25),borderwidth=0, highlightthickness =0).place(x = x, y = y)
x = x + 185
i = i + 1
x, y = 125, y + 130
while i < 32:
Button(second_frame, text = i, font = ("arial", 25),borderwidth=0, highlightthickness =0).place(x = x, y = y)
x = x + 185
i = i + 1
March22()
screen.mainloop()

Tkinter How can I withdraw and show a window?

I have successfully made a 'next' and 'back' button for the firstDicePage and secondDicePage functions so that I could return to either of the pages when needed. However, I am trying to avoid making multiple pages pop up every time I hit those buttons. I heard of .withdraw(), but I am unsure how to apply it in this code.
from tkinter import *
from tkinter import simpledialog
from tkinter import ttk
from tkinter import filedialog
from tkinter.font import Font
import tkinter as tk
import from PIL import Image, ImageTk
import tkinter
import sys
import random
def firstDicePage():
firstWindow = tkinter.Toplevel(window) # Create new window.
firstWindow.title("Dice Roller Generator")
# Change Icon photo
image = PhotoImage(file = "C:\\Users\\alexi\\Desktop\\Project Photos\\Dice logo.png")
firstWindow.iconphoto(False, image)
# Center
app_width = 900
app_height = 600
screen_width = firstWindow.winfo_screenwidth()
screen_height = firstWindow.winfo_screenheight()
x = (screen_width / 2) - (app_width / 2)
y= (screen_height / 2) - (app_height / 2)
firstWindow.geometry(f'{app_width}x{app_height}+{int(x)}+{int(y)}')
Label(firstWindow, text = "\n\nSelect How Many Dice You Want to Roll!", font='Helvetica 16 bold').pack()
img = ImageTk.PhotoImage(Image.open("Dice.png"))
my_label = Label(firstWindow, image=img)
my_label.img = img # Save reference to image.
my_label.pack()
counter = tkinter.IntVar()
def increase():
counter.set(min(10, counter.get() + 1))
def decrease():
counter.set(max(0, counter.get() - 1))
lbl = Label(firstWindow, textvariable = counter, font='Helvetica 16 bold')
lbl.place(x=450, y=330)
btn1 = Button(firstWindow, text="+", font='Helvetica 16 bold', padx = 8, pady = 5, command = increase, fg="dark green", bg = "white")
btn1.place(x=499, y=320)
btn2 = Button(firstWindow, text ="-", font='Helvetica 16 bold', padx = 11.1, pady = 5, command = decrease, fg="dark green", bg = "white")
btn2.place(x=375, y=320)
btn3 = Button(firstWindow, text = "Next", font='Helvetica 16 bold', command=lambda: secondDicePage())
btn3.place(x = 800 , y = 530)
def secondDicePage():
secondWindow = tkinter.Toplevel(window) # Create new window.
secondWindow.title("Dice Roller Generator")
# Change Icon photo
image = PhotoImage(file = "C:\\Users\\alexi\\Desktop\\Project Photos\\Dice logo.png")
secondWindow.iconphoto(False, image)
# Center
app_width = 900
app_height = 600
screen_width = secondWindow.winfo_screenwidth()
screen_height = secondWindow.winfo_screenheight()
x = (screen_width / 2) - (app_width / 2)
y= (screen_height / 2) - (app_height / 2)
secondWindow.geometry(f'{app_width}x{app_height}+{int(x)}+{int(y)}')
btn3 = Button(secondWindow, text = "Back", font='Helvetica 16 bold', command=lambda: firstDicePage())
btn3.place(x = 30 , y = 530)
def secondButton():
# Toplevel object which will
# be treated as a new window
secondWindow = Toplevel(window)
secondWindow.title("Dice Roller Generator")
# Change Icon photo
img = PhotoImage(file = "C:\\Users\\alexi\\Desktop\\Project Photos\\Dice logo.png")
secondWindow.iconphoto(False, img)
# Center
app_width = 600
app_height = 400
screen_width = secondWindow.winfo_screenwidth()
screen_height = secondWindow.winfo_screenheight()
x = (screen_width / 2) - (app_width / 2)
y= (screen_height / 2) - (app_height / 2)
secondWindow.geometry(f'{app_width}x{app_height}+{int(x)}+{int(y)}')
# A Label widget to show in top-level
Label(secondWindow, text = "\n\nSelect What/How Many Dice You Want to Roll!", font='Helvetica 16 bold').pack()
# Create the window(root interface)
window = Tk()
# Create a title
window.title("Dice Roller Generator")
# Change Icon photo
img = PhotoImage(file = "C:\\Users\\alexi\\Desktop\\Project Photos\\Dice logo.png")
window.iconphoto(False, img)
# Center
app_width = 600
app_height = 400
screen_width = window.winfo_screenwidth()
screen_height = window.winfo_screenheight()
x = (screen_width / 2) - (app_width / 2)
y= (screen_height / 2) - (app_height / 2)
window.geometry(f'{app_width}x{app_height}+{int(x)}+{int(y)}')
# Create a label widget
lbl = Label(window, text = "\n\n\nChoose the Kind of Die You Want to Roll", font='Helvetica 16 bold')
#Packing(size), Shove it in the screen
lbl.pack()
btn1 = Button(window, text = "Regular Dice", padx = 15, pady = 15, command = firstDicePage)
btn1.place(x=145, y=130)
btn2 = Button(window, text = "D&D Dice", padx = 20, pady = 15, command = secondButton)
btn2.place(x=355, y=130)
btn3 = Button(window, text="Exit", padx = 15, pady = 5, command = window.quit)
btn3.place(x=275, y=335)
window.mainloop()

tkinter set scale (create a 2x2 box)

I want to create a 2 by 2 box in python's tkinter, which will be my "world".
Is there a way to set the X and Y axes on the "world"?
Something like:
setXscale(-1.0, +1.0);
setYscale(-1.0, +1.0);
This can be done with the .pack() method as can be seen below:
from tkinter import *
root = Tk()
top = Frame(root)
bottom = Frame(root)
topleft = Frame(top)
topright = Frame(top)
bottomleft = Frame(bottom)
bottomright = Frame(bottom)
lbl1 = Label(topleft, text="topleft")
lbl2 = Label(topright, text="topright")
lbl3 = Label(bottomleft, text="bottomleft")
lbl4 = Label(bottomright, text="bottomright")
top.pack(side="top")
bottom.pack(side="bottom")
topleft.pack(side="left")
topright.pack(side="right")
bottomleft.pack(side="left")
bottomright.pack(side="right")
lbl1.pack()
lbl2.pack()
lbl3.pack()
lbl4.pack()
root.mainloop()
This creates a top frame and a bottom frame, each of which contain a left and right frame.
These frames are then packed in their respective side.
Alternatively, this can be done a lot easier with .grid() like this:
from tkinter import *
root = Tk()
topleft = Frame(root)
topright = Frame(root)
bottomleft = Frame(root)
bottomright = Frame(root)
lbl1 = Label(topleft, text="topleft")
lbl2 = Label(topright, text="topright")
lbl3 = Label(bottomleft, text="bottomleft")
lbl4 = Label(bottomright, text="bottomright")
topleft.grid(row = 0, column = 0)
topright.grid(row = 0, column = 1)
bottomleft.grid(row = 1, column = 0)
bottomright.grid(row = 1, column = 1)
lbl1.grid(row = 0, column = 0)
lbl2.grid(row = 0, column = 0)
lbl3.grid(row = 0, column = 0)
lbl4.grid(row = 0, column = 0)
root.mainloop()
Or like this:
from tkinter import *
root = Tk()
lbl1 = Label(root, text="topleft")
lbl2 = Label(root, text="topright")
lbl3 = Label(root, text="bottomleft")
lbl4 = Label(root, text="bottomright")
lbl1.grid(row = 0, column = 0)
lbl2.grid(row = 0, column = 1)
lbl3.grid(row = 1, column = 0)
lbl4.grid(row = 1, column = 1)
root.mainloop()

Python: Why doesn't my scrollbar show up using Tkinter?

Here is a snipet of my code:
dialog_canvas = Canvas(dialog_frame, scrollregion = (0, 0, 450, 265), bg = 'white')
dialog_canvas.pack(expand = YES, fill = BOTH, side = LEFT)
yscroll = ttk.Scrollbar(root, orient = VERTICAL, command = dialog_canvas.yview)
dialog_canvas.config(yscrollcommand = yscroll.set)
yscroll.grid(row = 1, column = 1, sticky = 'ns')

Categories