Tkinter网格不能放在屏幕死点的任何地方

问题描述 投票:0回答:1

所以我只需要一张背景照片,然后是某个地方的按钮。我有背景图片加载,但现在当我尝试按下按钮时,它只会直接进入屏幕中心。如果我将列或行设置为0以外的任何值,则该按钮将离开屏幕。

完整变量仅适用于用户是否需要全屏,我不希望它调整为任何特定大小,只需全屏或一半。但这无关紧要。

from Tkinter import *
from PIL import Image, ImageTk

full= False
pilImage= Image.open("test.png")
width, height = pilImage.size
new_size = width/2, height/2
resized_image = pilImage.resize(new_size, Image.ANTIALIAS)



root = Tk()
root.resizable(False, False)



if full:
    root.geometry("1920x1080")
    canvas= Canvas(root, width=1920, height=1080)
    background= ImageTk.PhotoImage(pilImage)
    root.attributes("-fullscreen", True)
    change_window_size_button= Button(root, text="Switch to windowed.", command=root.quit)

else:
    root.geometry("960x540")
    canvas= Canvas(root, width=960, height=540)
    background= ImageTk.PhotoImage(resized_image)
    change_window_size_button= Button(root, text="Switch to fullscreen.", command=root.quit)

canvas.grid(row=0)
label= Label(canvas, image=background)
label.grid(row=0)

change_window_size_button.grid(row=0, column=0)


root.mainloop()

![看起来像]:https://ibb.co/cycTFT

编辑:好的所以我发现了一些新的东西,问题是带有背景图像的画布是单元格的默认大小。因此,如果我让它经过第0行和第0列,它会从屏幕进入另一个单元格。如果我将其设为row = 0且column = 1,则按钮位于此处。

![row = 0 and column = 1]:https://ibb.co/jWt2aT

因此,我需要在保持图像大小的同时使细胞变小,这意味着我需要以某种方式使细胞占据多个细胞。我仍然不知道该怎么做......但是进步也是如此。

python python-2.7 tkinter tkinter-canvas tkinter-layout
1个回答
1
投票

我不确定这是否是您想要的,但我能够通过添加将按钮保持在Tkinter窗口内

root.grid_columnconfigure(0, weight=1)
root.grid_rowconfigure(0, weight=1)

然后改变按钮的网格位置

change_window_size_button.grid(row=1, column=0)

如果愿意,这将建立一个将小部件放在“主网格”(根)中的基础。

设置row=0将按钮放回中心。

如果你搞砸了这一点,你应该能够按下你想要的按钮。

tkinter button below image.

编辑:解释

© www.soinside.com 2019 - 2024. All rights reserved.