为什么我的tkinter Gui在右侧被切断?

2024-06-28 07:05:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚刚进入Python,一直在使用tkinter设计GUI,到目前为止,这很有趣。我一直在尝试使用框架,如果你愿意的话,我试图将屏幕分成3个“窗格”。出于某种原因,即使组合宽度小于总宽度,它仍然会超出屏幕的边界

就我个人而言,我不明白为什么右边的紫色边框被剪掉了

我有点怀疑我使用padx和pady的次数。我也很好奇它是否与grid_propagate或pack_propagate有关,这就是我为什么这么多次使用它的原因

感谢您的帮助

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() 

Tags: addcolumnrootframecommandgridrowbg
1条回答
网友
1楼 · 发布于 2024-06-28 07:05:24

我认为Python还没有意识到正确的屏幕分辨率,即使您使用geometry()进行设置。基于:

我想在root.state('zoomed')之后添加root.update_idletasks()行,看看这是否有助于改变它

如果不想每次都对几何体进行硬编码,可以尝试root.geometry(f"{root.winfo_width()}x{root.winfo_height()}"。考虑到边框占用一些像素和菜单栏在height上的空间,我认为您可能需要在此之后调整其他帧的widths/height

相关问题 更多 >