Lower monitor resolution causes tkinter window to be cut at right - python

I am using Tkinter gui, and at my default screen resolution (1920x1080), the dashboard displays fine as in image:
But when I run the same code on a lower resolution laptop (let's say 1366x768), right most widgets are cut as seen in picture below:
Here is my code which involves 2 windows root_tk is the main window which have a button called dashboard. When we press dashboard button, the dashboard window pops up. I have already tried the solutions Resolution problem of widgets with fixed lengths in Tkinter application
Why is my tkinter Gui cut off on the right?
But they do not work for me. Here is my code:
root_tk = customtkinter.CTk()
root_tk.state("zoomed")
root_tk.rowconfigure(3, weight=1)
root_tk.title("CustomTkinter Test")
frame_0 = Frame(root_tk,background="white")
frame_0.grid(row=0,column=0,sticky="ns",rowspan=4)
def dash():
dash_tk2 = customtkinter.CTkToplevel(root_tk)
dash_tk2.state("zoomed")
dash_tk2.update_idletasks()
dash_tk2.title("Dashboard")
dash_tk2.configure(bg='#302E2E')
frame_dash1 = customtkinter.CTkFrame(master=dash_tk2, corner_radius=15, fg_color="white")
frame_dash2 = customtkinter.CTkFrame(master=dash_tk2, corner_radius=15, fg_color="white")
frame_dash2a = customtkinter.CTkFrame(master=frame_dash2, corner_radius=15, fg_color="#FBBDBE")
frame_dash3 = customtkinter.CTkFrame(master=dash_tk2, corner_radius=15, fg_color="white")
frame_dash3a = customtkinter.CTkFrame(master=frame_dash3, corner_radius=15, fg_color="#FBBDBE")
frame_dash4 = customtkinter.CTkFrame(master=dash_tk2, corner_radius=15, fg_color="white")
frame_dash4a = customtkinter.CTkFrame(master=frame_dash4, corner_radius=15, fg_color="#FBBDBE")
frame_dash5 = customtkinter.CTkFrame(master=dash_tk2, corner_radius=15, fg_color="white")
frame_dash5a = customtkinter.CTkFrame(master=frame_dash5, corner_radius=15, fg_color="#FBBDBE")
frame_dash6 = customtkinter.CTkFrame(master=dash_tk2, corner_radius=15, fg_color="white")
blank_row1 = customtkinter.CTkFrame(master=dash_tk2, fg_color="yellow", height=10)
blank_col1 = customtkinter.CTkFrame(master=dash_tk2, fg_color="yellow", width=10)
blank_col2 = customtkinter.CTkFrame(master=dash_tk2, fg_color="yellow", width=10)
blank_col3 = customtkinter.CTkFrame(master=dash_tk2, fg_color="yellow", width=10)
blank_col4 = customtkinter.CTkFrame(master=dash_tk2, fg_color="yellow", width=10)
blank_col5 = customtkinter.CTkFrame(master=dash_tk2, fg_color="yellow", width=10)
blank_col6 = customtkinter.CTkFrame(master=dash_tk2, fg_color="yellow", width=10)
blank_row1.grid(row=0, column=0,columnspan=6)
blank_col1.grid(row=1, column=0)
frame_dash1.grid(row=1, column=1, sticky='ns',rowspan=2)
blank_col2.grid(row=1, column=2)
frame_dash2.grid(row=1, column=3, sticky='n',rowspan=2)
blank_col3.grid(row=1, column=4)
frame_dash3.grid(row=1, column=5, sticky='n',rowspan=2)
blank_col4.grid(row=1, column=6)
frame_dash4.grid(row=1, column=7, sticky='n',rowspan=2)
blank_col5.grid(row=1, column=8)
frame_dash5.grid(row=1, column=9, sticky='n',rowspan=2)
blank_col6.grid(row=1, column=10)
frame_dash6.grid(row=1, column=11, sticky='n')
frame_dash2a.grid(row=1, column=1, pady=(20, 10))
frame_dash3a.grid(row=1, column=1, pady=(20, 10))
frame_dash4a.grid(row=1, column=1, pady=(20, 10))
frame_dash5a.grid(row=1, column=1, pady=(20, 10))
blank_row2 = customtkinter.CTkFrame(master=frame_dash1, fg_color="orange", height=15)
blank_row2.grid(row=0,column=0,columnspan=2)
lab_tag = Label(frame_dash1, text="TAG", anchor=W, width=20, font='Tahoma 7', fg="black", bg='white')
lab_tag2 = Label(frame_dash1, text="FLOW Meter 4981", anchor=W, width=20, font='Tahoma 15', fg="black", bg='white')
blank_col7 = customtkinter.CTkFrame(master=frame_dash1, fg_color="orange", width=15)
blank_col7.grid(row=1, column=0,rowspan=15,sticky='ns')
lab_tag.grid(row=2, column=1, pady=(10, 0), sticky='w')
lab_tag2.grid(row=3, column=1, sticky='w')
lab_dashhead2 = Label(frame_dash2, text="MASS FLOW (KG/HR)", anchor=CENTER, font='Tahoma 10 bold', fg="white",bg='#EB3F3F')
lab_massflow2a = Label(frame_dash2a, text="19,999.999", anchor=CENTER, font='Tahoma 25 bold', fg="black", bg='#FBBDBE',width=10)
blank_col8 = customtkinter.CTkFrame(master=frame_dash2, fg_color="orange", width=10)
blank_col8.grid(row=1, column=0,rowspan=3)
blank_col9 = customtkinter.CTkFrame(master=frame_dash2, fg_color="orange", width=10)
blank_col9.grid(row=1, column=2,rowspan=3)
lab_dashhead2.grid(row=0, column=0, sticky='ew', pady=15, ipady=5, columnspan=3)
lab_massflow2a.grid(row=0, column=0, pady=20)
lab_dashhead3 = Label(frame_dash3, text="VOLUMETRIC FLOW (M\u00b3/HR)", anchor=CENTER, font='Tahoma 10 bold', fg="white",bg='#EB3F3F')
lab_massflow3a = Label(frame_dash3a, text="99,999", anchor=CENTER, font='Tahoma 25 bold', fg="black", bg='#FBBDBE',width=10)
blank_col10 = customtkinter.CTkFrame(master=frame_dash3, fg_color="orange", width=10)
blank_col10.grid(row=1, column=0,rowspan=3)
blank_col11 = customtkinter.CTkFrame(master=frame_dash3, fg_color="orange", width=10)
blank_col11.grid(row=1, column=2,rowspan=3)
lab_dashhead3.grid(row=0, column=0, sticky='ew', pady=15, ipady=5, columnspan=3)
lab_massflow3a.grid(row=0, column=0, pady=20)
lab_dashhead4 = Label(frame_dash4, text="STD.VOLUMETRIC FLOW (KG/HR)", anchor=CENTER, font='Tahoma 10 bold',fg="white", bg='#EB3F3F')
lab_massflow4a = Label(frame_dash4a, text="99999.99", anchor=CENTER, font='Tahoma 25 bold', fg="black", bg='#FBBDBE',width=10)
blank_col12 = customtkinter.CTkFrame(master=frame_dash4, fg_color="orange", width=10)
blank_col12.grid(row=1, column=0,rowspan=3)
blank_col13 = customtkinter.CTkFrame(master=frame_dash4, fg_color="orange", width=10)
blank_col13.grid(row=1, column=2,rowspan=3)
lab_dashhead4.grid(row=0, column=0, sticky='ew', pady=15, ipady=5, columnspan=3)
lab_massflow4a.grid(row=0, column=0, pady=20)
lab_dashhead5 = Label(frame_dash5, text="ENERGY FLOW (KG/HR)", anchor=CENTER, font='Tahoma 10 bold', fg="white",
bg='#EB3F3F')
lab_massflow5a = Label(frame_dash5a, text="99999.99", anchor=CENTER, font='Tahoma 25 bold', fg="black", bg='#FBBDBE',width=10)
blank_col14 = customtkinter.CTkFrame(master=frame_dash5, fg_color="orange", width=10)
blank_col14.grid(row=1, column=0,rowspan=3)
blank_col15 = customtkinter.CTkFrame(master=frame_dash5, fg_color="orange", width=10)
blank_col15.grid(row=1, column=2,rowspan=3)
lab_dashhead5.grid(row=0, column=0, sticky='ew', pady=15, ipady=5, columnspan=3)
lab_massflow5a.grid(row=0, column=0, pady=20)
lab_dashhead6a = Label(frame_dash6, text="REAL FACTORS", anchor=CENTER, font='Tahoma 10 bold',fg="white", bg='#7F7F7F',width=34)
lab_diffpx_un = Label(frame_dash6, text="MEASUREMENT FACTORS INVOLVED IN PROCESS (%)", anchor=CENTER,font='Tahoma 10', fg="black", bg='#C0C0C0',wraplength=200)
lab_diffpx_un_val = Label(frame_dash6, text="", anchor=CENTER, font='Tahoma 13 bold', fg="black",bg='#E8E8E8')
lab_dashhead6a.grid(row=0, column=0, sticky='ew', pady=(15,25), ipady=5)
lab_diffpx_un.grid(row=1, column=0, sticky='ew')
lab_diffpx_un_val.grid(row=2, column=0, sticky='ew')
dash_tk2.mainloop()

Good Question! You can actually make a parent frame for all those frames and widgets and add a scrollbar in x axis to that parent frame.. How to insert a scrollbar to a frame

You have to assign a weight to the grid cells you created. If you want every grid cell in row 1 of the top-level to be equally weighted, you do it like the following:
dash_tk2.grid_columnconfigure((0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11), weight=1)
Small window:
Wide window:

Related

When I click a global tkinter button, works only the first time

There are four buttons that hide or show a canvas. When I clicked the first time it work but then I don't really know it doesn't work. I made the buttons global because I need to reposition them depending on the canvas. How can I fix this? The function show_shares and show_DividendGrowth are the single ones that are used and they are creating some table cells and charts.
from tkinter import ttk
import pygsheets
from tkinter import *
from Shares import show_shares
from DividendGrowth import show_DividendGrowth
from MonthlyDividend import show_MonthlyDividend
from Evolution import showEvolution
client = pygsheets.authorize(service_account_file="dividend-portfolio.json")
sh = client.open('Portfolio')
wk1 = sh.sheet1
window = Tk()
window.title('Dividend Portfolio')
window.attributes('-fullscreen', True)
container = ttk.Frame(window)
container.pack(fill="both", expand=1)
canvas = Canvas(container)
scrollable_frame = ttk.Frame(canvas)
scrollbar_vertical = ttk.Scrollbar(container, orient="vertical", command=canvas.yview)
scrollbar_vertical.pack(side="right", fill="y")
canvas.configure(yscrollcommand=scrollbar_vertical.set)
canvas.bind('<Configure>', lambda e: canvas.configure(scrollregion=canvas.bbox("all")))
canvas.pack(fill=BOTH, expand=1)
canvas.create_window((0, 0), window=scrollable_frame, anchor="nw")
current_page = [0, 0, 0, 0]
shares_canvas = Canvas(scrollable_frame)
dividendGrowth_canvas = Canvas(scrollable_frame)
monthlyDividend_canvas = Canvas(scrollable_frame)
evolution_canvas = Canvas(scrollable_frame)
global shares_button, dividendGrowth_button, monthlyDividend_button, evolution_button
def placeButtons(master):
global shares_button, dividendGrowth_button, monthlyDividend_button, evolution_button
shares_button = Button(master, text="Actiuni", borderwidth=2,relief="solid", width=9, height=1, font=("Calibri", 13), bg="dodgerblue", fg="white")
dividendGrowth_button = Button(master, text="Dividende si cresteri", borderwidth=2, relief="solid", width=16, height=1, font=("Calibri", 13), bg="dodgerblue", fg="white")
monthlyDividend_button = Button(master, text="Dividende lunare", borderwidth=2, relief="solid", width=14, height=1, font=("Calibri", 13), bg="dodgerblue", fg="white")
evolution_button = Button(master, text="Evolutie", borderwidth=2,relief="solid", width=9, height=1, font=("Calibri", 13), bg="dodgerblue", fg="white")
def start(current_page):
if current_page[0] == 0:
current_page = [1, 0, 0, 0]
show_shares(shares_canvas)
shares_canvas.grid()
dividendGrowth_canvas.grid_forget()
monthlyDividend_canvas.grid_forget()
evolution_canvas.grid_forget()
placeButtons(shares_canvas)
shares_button.grid(row=102, column=0, columnspan=2, padx=6, pady=4, sticky=W)
dividendGrowth_button.grid(row=102, column=0, columnspan=3, padx=90, pady=4, sticky=E)
monthlyDividend_button.grid(row=102, column=1, columnspan=4, padx=47, sticky=W)
evolution_button.grid(row=102, column=3, columnspan=4, padx=56, sticky=W)
start(current_page)
def showShares(event, current_page):
start(current_page)
def showDividendGrowth(event, current_page):
if current_page[1] == 0:
current_page = [0, 1, 0, 0]
shares_canvas.grid_forget()
monthlyDividend_canvas.grid_forget()
evolution_canvas.grid_forget()
show_DividendGrowth(dividendGrowth_canvas)
dividendGrowth_canvas.grid()
placeButtons(dividendGrowth_canvas)
shares_button.grid(row=102, column=0, columnspan=2, padx=6, pady=4, sticky=W)
dividendGrowth_button.grid(row=102, column=0, columnspan=4, padx=101, pady=4, sticky=W)
monthlyDividend_button.grid(row=102, column=1, columnspan=5, padx=157, sticky=W)
evolution_button.grid(row=102, column=3, columnspan=4, padx=53, sticky=W)
scrollbar_orizontal = ttk.Scrollbar(container, orient="horizontal", command=canvas.xview)
scrollbar_orizontal.pack(side="bottom", fill="x")
canvas.configure(xscrollcommand=scrollbar_orizontal.set)
shares_button.bind('<Button-1>', lambda event: show_shares(event))
dividendGrowth_button.bind('<Button-1>', lambda event: showDividendGrowth(event, current_page))
window.mainloop()

How to solve the "bad window path name ".!labelframe.!canvas.!frame" error in tkinter python?

This is my code:
mycanvas = Canvas(self.search_result_frame)
mycanvas.pack(side=LEFT)
yscrollbar = ttk.Scrollbar(self.search_result_frame, orient="vertical", command=mycanvas.yview)
yscrollbar.pack(side=RIGHT, fill=Y)
mycanvas.configure(yscrollcommand=yscrollbar.set)
mycanvas.bind('<Configure>', lambda e: mycanvas.configure(scrollregion = mycanvas.bbox('all')))
self.sample_frame = Frame(mycanvas)
mycanvas.create_window((0,0), window=self.sample_frame, anchor=E)
for widget in self.search_result_frame.winfo_children():
widget.destroy()
if len(matching_bills) > 0:
for bill in matching_bills:
with open(f'{self.bill_folder}//{bill}//data//bill_details.json', 'r') as bill_json_file:
bill_details = json.loads(bill_json_file.read())
customer_name = bill_details["customer_details"][0]
payment_method = bill_details["payment_method"]
date_of_issue = bill_details["date_of_issue"]
date_of_issue = datetime.strptime(date_of_issue, "%d/%m/%Y")
date_of_issue = date_of_issue.strftime("%d %b %Y")
# # -------------------- Search Result Frame Contents
result_frame = Frame(self.sample_frame, bg=self.bg3, bd=5, relief=GROOVE)
result_frame.pack(fill=BOTH, pady=2)
result_billno_lbl = Label(result_frame, text=bill, bg=self.bg1, fg="#FFF", font=self.search_results_font1, padx=22, pady=3)
result_billno_lbl.grid(row=0, column=0, padx=50, pady=8, sticky=W)
billed_to_lbl = Label(result_frame, text=f"Billed To - {customer_name}", bg=self.bg1, fg="#FFF", font=self.search_results_font2, bd=2, relief=RAISED, padx=12, pady=3)
billed_to_lbl.grid(row=0, column=1, padx=80, sticky=W)
billed_type_lbl = Label(result_frame, text=f"Bill Type - {payment_method}", bg=self.bg1, fg="#FFF", font=self.search_results_font2, bd=2, relief=RAISED, padx=12, pady=3)
billed_type_lbl.grid(row=0, column=2, sticky=W)
issued_on_lbl = Label(result_frame, text=f"Issued On - {date_of_issue}", bg=self.bg1, fg="#FFF",
font=self.search_results_font2, bd=2, relief=RAISED, padx=12, pady=3)
issued_on_lbl.grid(row=0, column=3, padx=80, sticky=W)
view_btn = Button(result_frame, text="View", font="Comicsan 14", bd=2, relief=GROOVE, bg="#000", fg="#FFF", padx=1, command=lambda bill=bill: self.view_bill(bill))
view_btn.grid(row=0, column=4, padx=3, columnspan=2, sticky=W)
elif len(matching_bills) == 0:
for widgets in self.search_result_frame.winfo_children():
widgets.destroy()
no_result_lbl = Label(self.search_result_frame, text=f"No search result found for {bill_cat}", font=self.search_results_font1, bg=self.bg3, fg="#FFF")
no_result_lbl.pack(fill=X)
When I run it, it shows me the bad window path name ".!labelframe.!canvas.!frame error and when I try to do the same thing without object-oriented in tkinter then it works well !

tkinter resize GUI on different monitors

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.

Why is my tkinter scrollbar not working as intended? i hv removed the command line for the scrollbar

no error shown, i just removed the scrollbar command previously added as it wasnt working. i hope someone will be able to advise how to do this as i had gone thru multiple tutorials even those advises in stack overflow. i need to be able to scroll thru the entire window, below is the full code as requested.
import tkinter as tk
from tkinter import *
import time;
import datetime
from tkinter import ttk
from PIL import ImageTk, Image
import os
root = Tk()
root.title("Fibroscan Report")
root.geometry("1000x1200")
root.iconbitmap('d:/python_file/python_gui/zaclogo.ico')
frame0 = Frame(root, width=200, height=100)
frame0.pack()
frame0.pack_propagate(0)
frame1 = Frame(frame0, width=200, height=110, padx= 5, pady=5, bd=10,relief=RIDGE)
frame1.grid(row=0, column=0)
frame2 = Frame (frame0, width=200, padx= 5, pady=5, bd=10, relief= RIDGE)
frame2.grid(row=0, column=1)
frame3 = Frame(root, width=400, height=100)
frame3.pack()
frame3.pack_propagate(0)
frame4 = Frame(frame3, width=600, height=50, padx= 5, pady=5, bd=10, relief=RIDGE)
frame4.grid(row=0, column=0)
frame4.grid_propagate(0)
frame = Frame (root, width=200, height=100, padx=5, pady=15, relief=RIDGE)
frame.pack()
frame.pack_propagate(0)
framephoto = Frame (frame)
framephoto.grid()
canvas = Canvas(framephoto, width = 800, height = 324)
canvas.grid()
img = ImageTk.PhotoImage(Image.open("metavirchart2.png"))
canvas.create_image(107, 0, anchor=NW, image=img)
canvas.image = img
frame5 = LabelFrame (root,text="Fibroscan® Score Guide",font=('arial', 10), padx= 5, pady=5, bd=10, relief= RIDGE)
frame5.pack()
frame5.pack_propagate(0)
frame6 = Frame (frame5, width=230, height=50, padx= 5, pady=5, bd=10, relief= RIDGE)
frame6.grid(row=1, column=0)
frame6.grid_propagate(0)
frame7 = Frame (frame5, width=230, height=115, padx= 5, pady=5, bd=10, relief= RIDGE)
frame7.grid(row=2, column=0)
frame7.grid_propagate(0)
frame8 = Frame (frame5, width=230, height=50, padx= 5, pady=5, bd=10, relief= RIDGE)
frame8.grid(row=3, column=0)
frame8.grid_propagate(0)
frame9 = Frame (frame5, width=230, height=100, padx= 5, pady=5, bd=10, relief= RIDGE)
frame9.grid(row=4, column=0)
frame9.grid_propagate(0)
frame10 = Frame (frame5, width=515, height=50, padx= 5, pady=5, bd=10, relief= RIDGE)
frame10.grid(row=1, column=1)
frame10.grid_propagate(0)
frame11 = Frame (frame5, width=515, height=115, padx= 5, pady=5, bd=10, relief= RIDGE)
frame11.grid(row=2, column=1)
frame11.grid_propagate(0)
frame12 = Frame (frame5, width=515, height=50, padx= 5, pady=5, bd=10, relief= RIDGE)
frame12.grid(row=3, column=1)
frame12.grid_propagate(0)
frame13 = Frame (frame5, width=515, height=100, padx= 5, pady=5, bd=10, relief= RIDGE)
frame13.grid(row=4, column=1)
frame13.grid_propagate(0)
frameguide = Frame (frame5)
frameguide.grid(row=0, column=0)
frameguide.grid_propagate(0)
frame14 = Frame (frame13)
frame14.grid(row=0, column=1)
frame14.grid_propagate(0)
identity = StringVar()
fullname = StringVar()
refdr = StringVar(root)
datescan = StringVar()
dob = StringVar()
operator = StringVar()
operator.set('Insert Operator Name')
kpa = IntVar()
metavir = StringVar()
idrtype = StringVar()
idrtxt = StringVar()
lblidentity = Label(frame1, text="Patient ID", font=('arial', 10, 'bold'))
lblidentity.grid(row=0, column=0, sticky=W)
txtidentity = Entry(frame1, width=24, textvariable=identity, font=('arial', 10))
txtidentity.grid(row=0, column=1)
lblfullname = Label(frame1, text="Patient Name", font=('arial', 10, 'bold'))
lblfullname.grid(row=1, column=0, sticky=W)
txtfullname = Entry(frame1, width=24, textvariable=fullname, font=('arial', 10))
txtfullname.grid(row=1, column=1)
lbldob = Label(frame1, text="Date of Birth", font=('arial', 10, 'bold'))
lbldob.grid(row=2, column=0, sticky=W)
txtdob = Entry(frame1, width=24,textvariable=dob, font=('arial', 10))
txtdob.grid(row=2, column=1)
lblrefdr = Label(frame1, text="Referring Doctor", font=('arial', 10, 'bold'))
lblrefdr.grid(row=3, column=0, sticky=W)
txtrefdr = ttk.Combobox(frame1, width=24,
values=[
"Operator1",
"Operator2",
"Operator3",
"Operator4",
"Operator5",
"Operator6",
"Operator7"], state = "readonly")
txtrefdr.grid(row=3, column=1)
txtrefdr.current(0)
lbldatescan = Label(frame2, text="Date of Scan", font=('arial', 10, 'bold'))
lbldatescan.grid(row=0, column=0, sticky=W)
txtdatescan = Entry(frame2, width=24, textvariable=datescan, font=('arial', 10))
txtdatescan.grid(row=0, column=1)
lblindi = Label(frame2, text="Indication", font=('arial', 10, 'bold'))
lblindi.grid(row=1, column=0, sticky=W)
txtindi = ttk.Combobox(frame2,width=24,
values=[
"Hepatitis B*",
"HCV-HIV co-infection*",
"Hepatitis C",
"Hepatitis C*",
"Chronic Cholestatic Diseases*",
"Alcohol**",
"Others",
"NAFLD***"], state="readonly")
txtindi.grid(row=1, column=1)
txtindi.current(0)
lblgender = Label(frame2, text="Gender", font=('arial', 10, 'bold'))
lblgender.grid(row=2, column=0, sticky=W)
gender = ["Male", "Female", "Unspecified"]
txtgender = ttk.Combobox(frame2, width=24,
values=gender, state="readonly")
txtgender.grid(row=2, column=1)
txtgender.current(0)
lblop = Label(frame2, text="Operator", font=('arial', 10, 'bold'))
lblop.grid(row=3, column=0, sticky=W)
txtop = Entry(frame2, width=24, textvariable=operator, font=('arial', 10))
txtop.grid(row=3, column=1)
lbltitle = Label(frame4, text="YOUR FIBROSCAN® SCORE (Median LSM) is: ")
lbltitle.grid(row=0, column=0)
lblkpa = Label(frame4, text="KPa ")
lblkpa.grid(row=0, column=2)
txtkpa = Entry(frame4, textvariable=kpa, font=('arial', 10), width=5)
txtkpa.grid(row=0, column=1)
lblmeta = Label(frame4, text=" Metavir Stage: ")
lblmeta.grid(row=0, column=3)
metavir_score = ["F0", "F0-F1", "F1", "F1-F2", "F2", "F2-F3", "F3-F4", "F4"]
txtmeta = ttk.Combobox(frame4, width=10,
values=metavir_score, textvariable=metavir, state="readonly")
txtmeta.grid(row=0, column=4)
txtmeta.current(0)
lbltitle2 = Label(frame6,
text="Underlying Liver Disease", font=('arial', 10, 'bold'))
lbltitle2.grid(sticky=E)
lbltitle3 = Label(frame7,
text="Assessment", font=('arial', 10, 'bold'))
lbltitle3.grid(sticky=W)
lbltitle4 = Label(frame8,
text="Clinical Interpretation", font=('arial', 10, 'bold'))
lbltitle4.grid(sticky=W)
lbltitle5 = Label(frame9,
text="Recommended Clinical Actions", font=('arial', 10, 'bold'))
lbltitle5.grid(sticky=W)
lbltitleudl = Label(frame10,
text="next to UDL", font=('arial', 10, 'bold'))
lbltitleudl.grid()
lbltitle3a = Label(frame11,
text="Fibroscan Median LSM =", font=('arial', 10, 'bold'))
lbltitle3a.grid(row=0, column=0, sticky =W)
txtitle3a = Entry(frame11, width=16)
txtitle3a.grid(row=0, column=1)
lbltitle3a = Label(frame11,
text="Metavir Stage =", font=('arial', 10, 'bold'))
lbltitle3a.grid(row=1, column=0, sticky=W)
txtmetasg = ttk.Combobox(frame11, width=13,
values=metavir_score, state="readonly")
txtmetasg.grid(row=1, column=1)
txtmetasg.current(0)
lbltitle3a = Label(frame11,
text="Probe Type =", font=('arial', 10, 'bold'))
lbltitle3a.grid(row=2, column=0, sticky=W)
txtmeta = ttk.Combobox(frame11, width=13,
values=[
"S",
"M",
"XL",
"Others"], state="readonly")
txtmeta.grid(row=2, column=1)
txtmeta.current(0)
lbltitle3a = Label(frame11,
text="IQR/Median =", font=('arial', 10, 'bold'))
lbltitle3a.grid(row=3, column=0, sticky=W)
txtitle3a = Entry(frame11, width=16, textvariable=idrtype)
txtitle3a.grid(row=3, column=1)
lbltitle3a = Label(frame12,
text="Significant Fibrosis =", font=('arial', 13, 'bold'))
lbltitle3a.grid(row=0, column= 0)
txtmeta = ttk.Combobox(frame12, width=10,
values=[
"Yes",
"No"], state="readonly")
txtmeta.grid(row=0, column=1)
txtmeta.current(0)
fcolumn = Frame(frame13)
fcolumn.grid()
fcolumn1 = Frame(frame13)
fcolumn1.grid(sticky=W)
txtrca = ttk.Combobox(fcolumn, width=77, height=200,
values=[
"Repeat Fibroscan in 1 year",
"Patients with significant liver fibrosis have an increased risk of complications secondary \n to liver disease and development of HCC and should be considered \n for treatment of underlying aetiology or risk factor modification.",
"In patients with advanced fibrosis or cirrhosis, regular screening \n and surveillance for HCC is also recommended.",
])
txtrca.grid(row=0, column=0)
txtrca.current(0)
txtrca.grid_propagate(0)
lbliqr = Label(fcolumn1,
text="M Probe IQR/MEDIAN =", font=('arial', 10, 'bold'))
lbliqr.grid(row=1, column=0, sticky =W)
txtiqr = Entry(fcolumn1, width=16, textvariable=idrtxt)
txtiqr.grid(row=1, column=1)
lblguide = Label(fcolumn1,
text="*AASLD, APASL, EASL Guidelines", font=('arial', 10, 'bold'))
lblguide.grid(row=2, column=0, sticky =W)
root.mainloop()
any help will be appreciated.
no hate comments pls if u cant help or dun wish to help.
Try this
from tkinter import *
scroll_bar = Scrollbar(root)
scroll_bar.pack(side=RIGHT,fill=Y)

Tkinter, alignment - the endless question

Since some days I'll try to align these two items inside a Tkinter frame:
The pink part should be on the left and the green button on the right. With HtmlDivs and CSS a question of seconds, with TKinter a pain in the ...
Here is my python code:
import tkinter as tk
root = tk.Tk()
root.geometry("400x200")
buttons = tk.Frame(root)
buttons.pack(side="top", expand=True, fill='both')
label = tk.Label(buttons, text="Hello, world", anchor='w', background='pink')
b2 = tk.Button(buttons, text="EXIT", background='green')
label.grid(row=0, column=1, sticky='w', ipadx = '20', padx = '20')
b2.grid(row=0, column=3, sticky='e', ipadx = '20', padx = '20')
root.mainloop()
The final app will be running fullscreen on a 7" touch display.
The simplest solution is to use pack instead of grid, since you only have a single row of widgets inside of buttons. pack's strength is arranging things in a single row or a single column.
Just remove these two lines:
label.grid(row=0, column=1, sticky='w', ipadx = '20', padx = '20')
b2.grid(row=0, column=3, sticky='e', ipadx = '20', padx = '20')
... and replace them with this:
label.pack(side='left')
b2.pack(side='right')
Also, since you appear to be creating a toolbar, you want to leave expand as False and set fill to just "x", otherwise the toolbar will expand to fill the entire window:
buttons.pack(side="top", expand=False, fill='x')
Thx for your tips,
this is just a testcode to update my maincode.
Its not good to mix .pack and .grid, right ? If i do, the script crashes completely,
so ill have to re-do my mainscript i think.
This is what i need:
row: 1/2: label (it will be a real time clock) left 2/2: exit button (exit fullscreen) right
row: 1/3: image (weather), 2/3: weather data (label), 3/3: calendar
row: 4 buttons: 1. lightoff 2. light25%, 3.light50% 4.light100&
Thats it basically all is working, it just need to align it (im a coding hero g)
Since i have the frame from the code above, i thought i can easily add 2. & 3. row with another frames, but that does not seem to work ??
Preview:
https://i.stack.imgur.com/KzoqV.png
My (main)Code so far
root = Tk()
root.title('Model Definition')
root.config(background = "deepskyblue4")
root.attributes('-fullscreen',True)
root.bind('<Escape>',lambda e: root.destroy())
def currenttime():
string = strftime("%A, %d.%B.%Y %H:%M:%S")
lbl.config(text = string, background = 'deepskyblue4', fg='white', font=("colibri", 30))
lbl.after(1000, currenttime)
def light400():
FNULL = open(os.devnull, 'w')
subprocess.call(['gpio -g pwm 18 401'], stdout=FNULL, stderr=subprocess.STDOUT, shell=True)
def light425():
FNULL = open(os.devnull, 'w')
subprocess.call(['gpio -g pwm 18 425'], stdout=FNULL, stderr=subprocess.STDOUT, shell=True)
def light450():
FNULL = open(os.devnull, 'w')
subprocess.call(['gpio -g pwm 18 450'], stdout=FNULL, stderr=subprocess.STDOUT, shell=True)
def light475():
FNULL = open(os.devnull, 'w')
subprocess.call(['gpio -g pwm 18 500'], stdout=FNULL, stderr=subprocess.STDOUT, shell=True)
# Main frames
top_frame = Frame(root, bg='deepskyblue4', width=450, height=90, pady=3)
center = Frame(root, bg='deepskyblue2', width=50, height=40, padx=3, pady=3)
btm_frame = Frame(root, bg='deepskyblue4', width=450, height=20, pady=3)
btm_frame2 = Frame(root, bg='deepskyblue4', width=450, height=60, pady=3)
# layout all of the main containers
root.grid_rowconfigure(1, weight=1)
root.grid_columnconfigure(0, weight=1)
top_frame.grid(row=0, sticky="e", padx=(10, 0))
center.grid(row=1, sticky="nsew", pady=(10, 50))
btm_frame.grid(row=3, sticky="ew")
btm_frame2.grid(row=4, sticky="ew", padx=(10, 50))
# create the widgets for the top frame
lbl = Label(top_frame, background = 'deepskyblue4', anchor=W, justify=LEFT)
lbl.pack()
currenttime()
actionbutton = Button(top_frame, text="X", width=5, height=2, bg="deepskyblue4", fg="white", command=root.destroy)
hdr_left = Frame(top_frame, bg='deepskyblue4', width=100, height=190)
hdr_right = Frame(top_frame, bg='deepskyblue4', width=400, height=190, padx=3, pady=3)
# layout the widgets in the top frame
lbl.grid(row=0, column=1, sticky="w")
actionbutton.grid(row=0, column=2, sticky="e")
# create the center widgets
center.grid_rowconfigure(0, weight=1)
center.grid_columnconfigure(1, weight=1)
ctr_left = Frame(center, bg='deepskyblue2', width=100, height=190)
ctr_mid = Frame(center, bg='deepskyblue2', width=150, height=190, padx=3, pady=3)
ctr_right = Frame(center, bg='deepskyblue2', width=400, height=190, padx=3, pady=3)
ctr_left.grid(row=0, column=0, sticky="ns")
ctr_mid.grid(row=0, column=1, sticky="nsew")
ctr_right.grid(row=0, column=2, sticky="ns")
path = "icons/rain.png"
img = ImageTk.PhotoImage(Image.open(path))
panel = Label(ctr_left, image = img, bg="deepskyblue2")
panel.grid(row=0, columnspan=3)
#imgag = panel.pack(top_frame)
#CENTER ctr_mid
if x["cod"] != "404":
y = x["main"]
y2 = x["wind"]
currenttemp = y["temp"]
currentpressure = y["pressure"]
currenthumidiy = y["humidity"]
z = x["weather"]
weather_description = z[0]["description"]
currentwind = y2["speed"]
label1 = Label(ctr_mid,text='Karlsruhe', font = ('calibri', 30), background = 'deepskyblue2')
label2 = Label(ctr_mid,text='Temperatur: '+str(round(currenttemp-272.15))+' °C', font = ('calibri', 20), background = 'deepskyblue2')
label3 = Label(ctr_mid,text='Beschreibung: '+str(weather_description),font = ('calibri', 20), background = 'deepskyblue2')
label4 = Label(ctr_mid,text='Druck: '+str(currentpressure)+' hPa', font = ('calibri', 20), background = 'deepskyblue2')
label5 = Label(ctr_mid,text='Feuchtigkeit: '+str(currenthumidiy)+' %',font = ('calibri', 20), background = 'deepskyblue2')
label6 = Label(ctr_mid,text='Wind: '+str(currentwind)+' m/Sek',font = ('calibri', 20), background = 'deepskyblue2')
label1.grid(row=0, column=0, sticky="nw")
label2.grid(row=1, column=0, sticky="nw")
label3.grid(row=2, column=0, sticky="nw")
label4.grid(row=3, column=0, sticky="nw")
label5.grid(row=4, column=0, sticky="nw")
label6.grid(row=5, column=0, sticky="nw")
# btm_frame2 widgets
licht = Label(btm_frame2, text='Licht:', width=5, height=1, bg="deepskyblue4", fg='white', font=("colibri", 20))
button = Button(btm_frame2, command=light400, text="AUS", width=5, height=1, bg="deepskyblue2", fg="white")
button2 = Button(btm_frame2, text="25 %", command=light425, width=5, height=1, bg="deepskyblue2", fg="white")
button3 = Button(btm_frame2, text="50%", command=light450, width=5, height=1, bg="deepskyblue2", fg="white")
button4 = Button(btm_frame2, text="100%", command=light475, width=5, height=1, bg="deepskyblue2", fg="white")
licht.grid(row=0, column=0, sticky="nw")
button.grid(row=0, column=1, sticky="nw")
button2.grid(row=0, column=2, sticky="nw")
button3.grid(row=0, column=3, sticky="nw")
button4.grid(row=0, column=4, sticky="nw")
actionbutton.grid(row=0, column=6, sticky="nw")
root.mainloop()

Categories

Resources