python Tkinter 与其他小部件平等地占据图像

python Tkinter occupy image equally to other widget

我有这个代码。

class App(object):
    def __init__(self):
        self.root = Tk()
        self.root.attributes('-zoomed', True)
        self.root.grid_rowconfigure(0, weight=1)
        self.root.grid_columnconfigure(0, weight=1)
        self.root.grid_columnconfigure(1, weight=1)

        f1 = Frame(self.root, bd=1, bg="green")
        f3 = Frame(self.root, bd=1, bg="blue")

        self.image = Image.open("default.png")
        self.photo = ImageTk.PhotoImage(self.image)
        self.label = Label(image=self.photo)
        self.label.grid(row=0, column=1, sticky="nsew")

        f1.grid(row=0, column=0, sticky="nsew")
        f3.grid(row=1, column=0, columnspan=2, sticky="ew")

app = App()
app.root.mainloop()

输出是

如何让图片与左框等量?

在提供解决方案之前,我想强调几点:

  1. 您没有使用框架 f3 做任何事情。 运行 你的程序甚至不显示。所以我将在下面的解决方案中忽略它。
  2. 您为主框架attributes设置了属性-zoomed。我不知道您要通过使用它来实现哪个目标。无论如何,有许多线程表明它只能工作 Windows 和一些 Debian 发行版,如 Ubuntu。虽然我没有遇到这个问题,但是,正如您在下面提供的第一个 link 中所读到的,这些参数是特定于平台的。所以为了代码的可移植性,你可以使用-fullscreen instead. But here again, especially for your case, it will affect even the taskbar of your GUI which will not be displayed and thus you can not close your program by clicking on the close button. So in my solution I will rather use winfo_screenheight() and winfo_screenwidth() to set self.root to the size of the screen of your machine and call them inside the geometry()方法。

让我们将您的问题分解成更小的问题:

  1. 首先要解决的问题是如何设置第一帧f1self.label等宽。这是通过简单地将它们中的每一个的宽度选项设置为屏幕宽度的一半来完成的:width = self.root.winfo_screenwidth()/2

  2. 完成此操作后,您将需要解决您在评论中阐明的第二个问题。为此,您需要通过应用 resize() method on the image, and pass to it the dimensions of the image which must be the ones of your label along with the constant PIL.Image.ANTIALIAS.

  3. 来调整 self.image 大小以适应 self.label 大小

完整节目

所以这是完整的程序:

'''
Created on May 5, 2016

@author: billal begueradj
'''
import Tkinter as Tk
import PIL.Image
import PIL.ImageTk

class App(object):
    def __init__(self):
        self.root = Tk.Tk()       
        self.root.grid_rowconfigure(0, weight=1)
        self.root.grid_columnconfigure(0, weight=1)
        self.root.grid_columnconfigure(1, weight=1)        
        # Get width and height of the screen
        self.h= self.root.winfo_screenheight()
        self.w= self.root.winfo_screenwidth()
        # Set the GUI's dimensions to the screen size
        self.root.geometry(str(self.w) + "x" + str(self.h))
        # Set the  width of the frame 'f1' to the half of the screen width 
        self.f1 = Tk.Frame(self.root, bd=1, bg="green", width= self.w/2)
        self.f1.grid(row=0, column=0, sticky="nsew")        
        # Load the image
        self.image = PIL.Image.open("/home/Begueradj/mahomet_pedophile.jpg")       
        # Resize the image to fit self.label width 
        self.photo = PIL.ImageTk.PhotoImage(self.image)
        self.label = Tk.Label(self.root, image=self.photo,  width= self.w/2)
        self.label.grid(row=0, column=1, sticky="nsew")


# Launch the main program      
if __name__ == '__main__':
   app = App()
   app.root.mainloop()

演示

诺塔贝内

您的评论有些不准确。它并没有说明您是希望整个图片显示为与框架 f1 相同的大小,还是仅显示其所附加的标签 (self.label)。如果这是你想要做的,那么忽略前面程序中的这一行:

self.image = self.image.resize((self.w/2, self.h), PIL.Image.ANTIALIAS) 

演示

输出将如下所示(图像只是位于标签的中心):