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

rdlzhqv9  于 2021-08-25  发布在  Java
关注(0)|答案(1)|浏览(366)

我刚刚进入python,一直在使用tkinter设计GUI,到目前为止,这很有趣。我一直在尝试使用框架,如果你愿意的话,我试图将屏幕分成3个“窗格”。出于某种原因,即使组合宽度小于总宽度,它仍然会超出屏幕的边界。
就我个人而言,我不明白为什么右边的紫色边框被剪掉了。
我有点怀疑我使用padx和pady的次数。我也很好奇它是否与grid_propagate或pack_propagate有关,这就是我为什么这么多次使用它的原因。
感谢您的帮助。
结果

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

c3frrgcw1#

我承认除了知道如何安装它之外,我对tkinter几乎一无所知。然而,我复制了你的代码并尝试了一下。在玩过之后,我发现了以下信息:

width  = root.winfo_screenwidth()
height = root.winfo_screenheight()

源于如何将根窗口大小设置为屏幕宽度和高度的一半
我想这对我来说是可行的,但我在侧面有我的开始菜单,所以我从宽度中减去了足够多的像素,使它看起来很漂亮。对你有用吗?
我会用这两个函数来做类似的事情 root.geometry(f'{width}x{height}') 而不是在屏幕尺寸中硬编码。我把结果打印出来了 widthheight ,还没有找到原因,但它们与windows10报告的维度不同,所以我还没有找到原因。但基本上,您已经超出了python实际使用的维度。

相关问题