所以我只需要一张照片作为背景,然后在某个位置有一个按钮。我已经加载了背景图像,但是现在当我尝试放置按钮时,它只会直接进入屏幕中心。如果我将列或行设为 0 以外的任何值,则该按钮会离开屏幕。
full 变量仅用于用户是否想要全屏,我不希望它调整为任何特定大小,只是全屏或半屏。但这无关紧要。
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,这就是按钮所在的位置。
![行=0 和列=1]:https ://ibb.co/jWt2aT
所以我需要在保持图像大小的同时使单元格变小,这意味着我需要以某种方式使其占据多个单元格。我仍然不知道如何做到这一点......但进步不减。