简体   繁体   English

Tkinter网格不能放置在屏幕的任何中心

[英]Tkinter grid can't place anywhere but dead center of screen

So I just need a photo for a background, and then a button in a certain spot. 所以我只需要一张照片作为背景,然后在某个位置单击一个按钮即可。 I have the background image loading, but now when I try and place the button it will only go straight into the center of the screen. 我正在加载背景图片,但是现在当我尝试放置按钮时,它只会直接进入屏幕中心。 If I make the column or row anything other than 0, the button goes off the screen. 如果我将列或行的值设为0以外的值,则该按钮将不显示在屏幕上。

The full variable is just for if the user wants fullscreen or not, I don't want it to resize to any specific size, just full screen or half. full变量仅用于用户是否要全屏显示,我不希望它调整为任何特定大小,仅全屏显示或全屏显示。 But that's irrelevant. 但这无关紧要。

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

![What it looks like]: https://ibb.co/cycTFT ![看起来像什么]: https//ibb.co/cycTFT

EDIT: Ok so I found out something new, the problem is that the canvas with the background image is made the default size for the cells. 编辑:好的,所以我发现了新的东西,问题是带有背景图像的画布被设置为单元格的默认大小。 So if i make it go past row and column 0, it goes off the screen into another cell. 因此,如果我经过第0列和第0列,它将离开屏幕进入另一个单元格。 Here's where the button goes if i make it row=0 and column=1. 如果我将其设置为row = 0和column = 1,这是按钮的位置。

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

So I need to make the cells small while maintaining the size of the image, meaning I need to somehow make that take up multiple cells. 因此,我需要在保持图像大小的同时缩小单元格,这意味着我需要以某种方式使其占用多个单元格。 I still don't know how to do that... But progress none the less. 我仍然不知道该怎么做。。。但是还是要进步。

I'm not sure if this is what you wanted specifically, but I was able to keep the button within the Tkinter window by adding 我不确定这是否是您特别想要的,但是我可以通过添加将按钮保留在Tkinter窗口中

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

and then changing the grid location of the button like 然后像这样更改按钮的网格位置

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

this establishes a basis for which to place Widgets inside the "master grid" (root), if you will. 如果可以的话,这将为在“主网格”(根)中放置小部件奠定基础。

setting row=0 places the button back into the center. 设置row=0会将按钮放回中心。

if you mess with this a little bit, you should be able to get the button where you want it. 如果您对此感到有些困惑,则应该可以将按钮放置在所需的位置。

tkinter button below image. 图像下方的tkinter按钮。

edit: explanation 编辑:解释

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM