【发布时间】: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