【问题标题】:Using grid how do I not have frames push each other out of place使用网格我如何不让框架相互推开
【发布时间】:2019-01-30 23:27:52
【问题描述】:

解决这个问题已经超过一天了。

我希望我的顶部框架正对着图片。

当我不包括北部框架下方的第 2 部分框架时,效果很好。 我已经查找并尝试使用 column_configure,尝试过重量。 老实说,我可能用错了。

我知道这里有修复,但我还没有想到如何让它发挥作用。

如果可能,您能否不使用 CLASSES 或许多花哨的 FUNCTIONS。 老实说,在这个阶段,我现在对他们很弱。 提前感谢您花时间阅读并提供任何帮助。

from tkinter import *

import os
import PIL.Image
import random
import sys
import tkinter as tk

global logo
global po

# path to image file
home = os.path.expanduser('~')
dt = desktop_top = os.path.join(home,'Desktop')
p3p = python_3_projects = os.path.join(dt,'Python 3 Projects')
ilfru = images_labeled_for_reuse = os.path.join(p3p,'Images Labeled for Reuse')
gp = gurps_path = os.path.join(ilfru,'GURPS')
gg = gurps_gif = os.path.join(gp,'GIF')

fc = 1
# Count number of file in GURPS/GIF folder
for root, dirs, files in os.walk(gg):
    for filenames in files:
        fc +=1

# Pick a random number based off number of files
rn = random.randint(1,fc)

# Cycle through files and assign the random file to logo
fc = 1
# Count number of file in GURPS/GIF folder
for root, dirs, files in os.walk(gg):
    for filenames in files:
        if fc == rn:
            logo = os.path.join(gg,filenames)
        fc +=1

# Get Image Size
# po PIL Open
po = open(logo,"rb")
img = PIL.Image.open(po)

# Image Width, Image Height
iw,ih = img.size

ih = ih + 5

root = tk.Tk()

# Round Down Nearest tenth
def rd(num,divisor):
    x = (num/divisor)-(num%divisor)
    x = int(x)
    return x

# Get Screen Dimensions
sh = screen_height = root.winfo_screenheight()
sw = screen_width = root.winfo_screenwidth()

# Window size
rg = str(sw)+'x'+str(sh)
root.geometry(rg)

# Create all frames
nwfh = rd(sh, 10)
nwfw = rd(sw, 4)

equal_sides = nwfh + nwfw
equal_sides = rd(equal_sides,4)

ntf = rd(sw-equal_sides,1)

# Frames

spacer_width = rd(equal_sides,10)

frame0 = Frame(root,bg='black',width=sw,height=sh,padx=5,pady=5)
frame0.grid(row=0,column=0)

north_west_top_frame = Frame(frame0, bg='cyan', width=equal_sides, height=equal_sides)

frame_spacer0 = Frame(frame0, bg='beige', width=spacer_width, height=ih)

northern_top_frame = Frame(frame0,bg='alice blue', width=ntf,height=ih,padx=15,pady=15)

# Layout all frames

# Trying configure
# northern_top_frame.grid_columnconfigure(1,weight=1)


# Frame Section 1
north_west_top_frame.grid(row=0,column=0, sticky="nw")

frame_spacer0.grid(row=0, column=1, sticky="w")

northern_top_frame.grid(row=0,column=2)


# Put logo in nw frame
glogo = tk.PhotoImage(file=logo)
gurps_logo = tk.Label(north_west_top_frame, compound = tk.CENTER, text='GURPS\nCHARACTER\nSHEET',fg = "red", font="Helvetica 10 bold", image=glogo).grid(row=0,column=0)

l1 = tk.Label(northern_top_frame, anchor='w', text="Name: ", font = "Helvetica 10 bold",width=17)
l1.grid(row=0, column=0)
e1 = Entry(northern_top_frame)
e1.grid(row=0,column=1)

# Spacer Width = pic prame width -
# ih is northern frame height
# need label height

spacer_height = ih-30

# spacer
# s1 = Frame(northern_top_frame,bg='firebrick', width=ntf,height=spacer_height)
# s1.grid(row=1,column=1, columnspan=11, sticky=tk.W+tk.E)


l4 = tk.Label(northern_top_frame, anchor='w', text="Ht: ", font = "Helvetica 10 bold", width=17)
l4.grid(row=2, column=0)
e4 = Entry(northern_top_frame)
e4.grid(row=2,column=1)



l9 = tk.Label(northern_top_frame, anchor='w', text="Appearance: ", font = "Helvetica 10 bold", width=17)
l9.grid(row=4, column=0)
e9 = Entry(northern_top_frame)
e9.grid(row=4,column=1, columnspan=11, sticky=tk.W+tk.E)

# Section 2
s2 = Frame(frame0,bg='firebrick', width=ntf)
s2.grid(row=5,column=0, sticky="w")


l10 = tk.Label(s2, text="LEVEL")
l10.grid(row=6,column=1)


root.mainloop()

【问题讨论】:

  • 请修正您的代码格式,并尝试将代码缩减为minimal reproducible example。例如,我们不需要顶部和底部的几十个空白行,我们可能不需要像 rd 这样的函数或几十个标签,如果您的问题只是使框架与图像对齐.
  • 将尝试编辑掉大部分空格
  • 请尝试做更多的事情。我们不需要你所有的代码,我们只需要足够的代码来复制问题。
  • 这行得通还是我应该剪得更多?
  • 我尽可能地减少了它,留下了 rd 和 random 函数。因为当我尝试在没有它的情况下运行代码时,它会破坏它更多。我相信你的判断,我倾向于陷入我切割太多或不够。无论我尝试什么都营地。

标签: python-3.x layout tkinter grid frame


【解决方案1】:

如果我理解正确,您的问题基本上可以归结为:米色和白色框架被红色框架推到右侧,尽管青色框架旁边有足够的空间

Minimal Complete Verifiable Example 可以这么简单:

from tkinter import *

root = Tk()

frame0 = Frame(root, bg='black', width=500, height=300, padx=5, pady=5)
frame0.grid(row=0, column=0)


north_west_top_frame = Frame(frame0, bg='cyan', width=150, height=150)
frame_spacer0 = Frame(frame0, bg='beige', width=50, height=150)
northern_top_frame = Frame(frame0, bg='alice blue', width=300, height=150)

north_west_top_frame.grid(row=0, column=0, sticky="nw")
frame_spacer0.grid(row=0, column=1, sticky="w")
northern_top_frame.grid(row=0, column=2)


s2 = Frame(frame0, bg='firebrick', width=500, height=150)
s2.grid(row=5, column=0, sticky="w")

root.mainloop()

这里的问题是如何定义网格。您将north_west_top_frames2 都放在最左边的第0 列中。你把frame_spacer0 放在右边一列,northern_top_frame 比这更远一列。每列都会拉伸以适应您放入其中的所有内容,因此第一列(第 0 列)与 s2 一样宽。带有米色框的下一列紧挨着第一列,因此它将从红色框结束的地方开始。

您似乎想要的是红色框架不仅在第 0 列,而且在第 1 列和第 2 列,在米色和白色框架下。为此,请使用columnspan:

s2.grid(row=5, column=0, columnspan=3, sticky="w")

当您执行此操作时,您会看到红色框一直跨越青色、米色和白色框:

【讨论】:

  • 这是一个很棒的答案。很高兴您花时间将问题归结为本质,并提供了漂亮的屏幕截图。您可能希望在答案中包含由代码生成的屏幕截图以进行比较。
  • 非常感谢您修复了它。
  • 是的,Bryan 是对的,你解释得很好,连我的老头脑都能理解。再次感谢。到我的下一个问题。 (进步!!):)
  • @BryanOakley 感谢您的客气话。我刚刚添加了一个“之后”屏幕截图。
  • @Warbit 不客气,我很高兴能帮上忙。除了帮助您更多地了解 tkinter grid,我还希望我的回答能让您深入了解如何制作 minimal reproducible example。如您所见,在展示问题的同时,我使用的代码比您少得多,我删除了与您遇到的问题无关的所有内容。了解如何做到这一点非常有助于提出其他人(有时甚至是您自己)更容易回答的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-09-20
  • 2017-03-25
  • 1970-01-01
  • 2011-07-08
  • 1970-01-01
  • 2011-03-23
  • 2013-08-27
相关资源
最近更新 更多