2

I just got into Python and have been using tkinter to design GUIs which has been fun so far. I've been experimenting with the frames, and I was trying to separate the screen into 3 'panes' if you will. For some reason, even though the combined widths are less than the total width, it still extends past the bounds of the screen.

For the life of me I can't figure out why the purple frame is cut off on the right.

I am somewhat suspicious of the amount of times I've used padx and pady. Also am curious if it's related to grid_propagate or pack_propagate, which is why I have used it so many times.

Any help is appreciated.

RESULT

import tkinter as tk
from tkinter import *
from tkinter import filedialog

import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure


def close():
    exit()

#--------------------------------------------------------------
# Root Stuff
root = tk.Tk()
root.title('rough frame gui test v1')
root.geometry('1920x1080')
root.attributes('-fullscreen', False)
root.state('zoomed')
root.iconphoto(False, tk.PhotoImage(file='C:/Users/Trevor/OneDrive - Providence College/Research/Summer 2021/Single Py Files/spec_icon.png'))



#--------------------------------------------------------------
# A couple Random Functions
def donothing():
    print('nothing')

def close():
    root.destroy()
#---------------------------------------------------------------
# 0 - Main Frame
#root = Frame(root)
#root.grid(row=0, column=0, sticky="nswe")


#---------------------------------------------------------------
# 1 - Navigation Frame
frameNav = Frame(root, bg="blue", height=500, width=480)
frameNav.grid(row=0, column=0, sticky=N)
frameNav.grid_propagate(True)

global canvasNav
canvasNav = Canvas(frameNav, bg="white", height=500, width=480, bd=2, relief=SUNKEN)
canvasNav.grid(row=0, column=0, sticky="nswe", padx=5, pady=5)
canvasNav.grid_propagate(False)

navTitle = Label(canvasNav, bg='white', text="Current Database:", bd=2, anchor=CENTER)
navTitle.grid(row=0, column=0, padx=5, pady=5)

navPane = Label(canvasNav, bg='white', text="NAVIGATION PANE", bd=2, anchor=CENTER)
navPane.grid(row=2, column=0, padx=5, pady=5)

#---------------------------------------------------------------
# 2 - Graph Frame
frameGraph = Frame(root, bg="green", height=500, width=960)
frameGraph.grid(row=0, column=1, sticky=N)
frameGraph.grid_propagate(True)

global canvasGraph
canvasGraph = Canvas(frameGraph, bg="white", height=500, width=960, bd=2, relief=SUNKEN)
canvasGraph.grid(row=0, column=0, sticky="nswe", padx=5, pady=5)
canvasGraph.grid_propagate(False)

loadGraph = Button(canvasGraph, text="Load Graph", bd=2, anchor=CENTER)
loadGraph.grid(row=0, column=0, sticky=S, padx=5, pady=5)
#---------------------------------------------------------------
# 3 - Tasks Frame
frameTasks = Frame(root, bg="purple", height=500, width=400)
frameTasks.grid(row=0, column=2, sticky=N)
frameTasks.grid_propagate(True)

global bFrameTasks
bFrameTasks = Canvas(frameTasks, bg="white", height=500, width=400, bd=2, relief=SUNKEN)
bFrameTasks.grid(row=0, column=0, sticky="nswe", padx=5, pady=5)
bFrameTasks.grid_propagate(True)

#---------------------------------------------------------------
# FUNCTION TO HIDE WINDOWS (TASKS)
#
def toggleTasks():
    try:
        global bFrameTasks
        print(bFrameTasks.winfo_exists())
        if bFrameTasks.winfo_exists() == 1:
            print("Destroying package...")
            bFrameTasks.destroy()
        else:
            bFrameTasks = tk.Frame(frameTasks, bg="white", height=500, width=480, bd=2, relief=SUNKEN)
            bFrameTasks.pack(padx=5, pady=5)
    except Exception as e:
        print(e)
        print('An error has occurred...')
#---------------------------------------------------------------
#end

#---------------------------------------------------------------
# FUNCTION TO HIDE WINDOWS (GRAPH)
# 
def toggleGraph():
    try:
        global canvasGraph
        print(canvasGraph.winfo_exists())
        if canvasGraph.winfo_exists() == 1:
            print('Destroying package...')
            canvasGraph.destroy()
        else:
            canvasGraph = tk.Canvas(frameGraph, bg="white", height=500, width=960, bd=2, relief=SUNKEN)
            canvasGraph.pack(padx=5,pady=5)
    except Exception as e:
        print(e)
        print('An error has occurred...')
#---------------------------------------------------------------
#end

#---------------------------------------------------------------
# FUNCTION TO HIDE WINDOWS (NAVIGATION)
#
def toggleNav():
    try:
        global canvasNav
        print(canvasNav.winfo_exists())
        if canvasNav.winfo_exists():
            print('Destroying...')
            canvasNav.destroy()
        else:
            canvasNav = tk.Canvas(frameNav, bg="white", height=500, width=480, bd=2, relief=SUNKEN)
            canvasNav.pack(padx=5,pady=5)
    except Exception as e:
        print(e)
        print('An error has occurred')
#---------------------------------------------------------------
#end

def fileExplore():
    dbFolder = filedialog.askdirectory(initialdir = '/', title = 'Select Database Folder')
    print(dbFolder) #working! just need to get it to display to a widget
    folderName = tk.Label(canvasNav, bg='gray', text=dbFolder)
    folderName.grid(row=1, column=0, sticky=N, padx=5, pady=5)

#---------------------------------------------------------------
# MENU
menuBar = Menu(root)
fileMenu = Menu(menuBar, tearoff=0)
menuBar.add_cascade(label="File", menu=fileMenu)
fileMenu.add_command(label="Open Database Folder", command=fileExplore)
fileMenu.add_command(label="Save", command=donothing)
fileMenu.add_command(label="Save as...", command=donothing)
fileMenu.add_separator()
fileMenu.add_command(label="Exit...", command=close)

hideMenu = Menu(menuBar, tearoff=0)
menuBar.add_cascade(label="Hide", menu=hideMenu)
hideMenu.add_checkbutton(label="Task Bar", command=toggleTasks)
hideMenu.add_checkbutton(label="Graph", command=toggleGraph)
hideMenu.add_checkbutton(label="Navigation", command=toggleNav)
#---------------------------------------------------------------

root.config(menu=menuBar)
root.mainloop() 
  • 1
    Can you please provide a minimal working example? Remove all of the code that isn't necessary to reproduce the problem. It makes it easier for us to debug your code. – TheLizzard Jul 08 '21 at 17:23
  • You have set the window's width to 1080 pixels, yet have three Canvases horizontally arranged within it having a total width of 1840 pixels. What did you expect to happen? – jasonharper Jul 08 '21 at 17:26
  • Also why are you calling `.grid` on widgets (`navTitle`, `navPane`) where their master is a canvas (`canvasNav`)? It doesn't make sense. – TheLizzard Jul 08 '21 at 17:26
  • @jasonharper the windows width is 1920 – Trevor Daron Jul 08 '21 at 17:50
  • @TheLizzard Like I said, I'm literally brand new to this. Can you explain what you mean? – Trevor Daron Jul 08 '21 at 17:50

1 Answers1

1

I think Python's not yet aware of the correct screen resolution even though you're setting it with geometry(). Based on :

I'd say add the line for root.update_idletasks() after the root.state('zoomed') to see if that helps to change it.

If you don't want to hardcode the geometry each time you can try root.geometry(f"{root.winfo_width()}x{root.winfo_height()}". I think you may want to adjust the widths/height of the other Frames after this as well in consideration of borders taking up some pixels and the space the menu bar would take on the height

ŽaMan
  • 396
  • 5
  • 17