【问题标题】:python Tkinter occupy image equally to other widgetpython Tkinter 与其他小部件一样占据图像
【发布时间】:2016-05-04 05:24:30
【问题描述】:

我有这个代码。

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

输出是

我怎样才能让图像在左边的框架中占据相等的位置?

【问题讨论】:

  • 但是图像已经均匀地定位在左(绿色)框架上,那么你的问题是什么?
  • 问题是我想要绿色宽度和图片宽度一样。

标签: python python-2.7 tkinter tkinter-canvas


【解决方案1】:

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

  1. 你没有使用框架f3做任何事情。运行程序时甚至不会显示它。因此,我将在下面的解决方案中简单地忽略它。
  2. 您为主框架'attributes 设置了属性-zoomed。我不知道您将通过使用它来实现哪个目标。无论如何,有许多线程表明它仅适用于 Windows 和一些 Debian 发行版,如 Ubuntu。不过,我没有遇到这个问题,但是,正如您可以在下面我提供的第一个链接中看到的那样,这些参数是特定于平台的。因此,为了代码的可移植性,您可以改用-fullscreen。但是在这里,特别是对于您的情况,它甚至会影响您的 GUI 的任务栏,它不会显示,因此您无法通过单击关闭按钮来关闭您的程序。所以在我的解决方案中,我宁愿使用winfo_screenheight()winfo_screenwidth()self.root 设置为您机器的屏幕大小,并在geometry() 方法中调用它们。

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

  1. 您需要解决的第一个问题是如何将第一帧f1self.label 设置为相等的宽度?这只需将它们中的每一个的宽度选项设置为屏幕宽度的一半即可完成:width = self.root.winfo_screenwidth()/2

  2. 完成后,您需要解决您在评论中阐明的第二个问题。为此,您需要通过在图像上应用resize() 方法来调整self.image 的大小以适合self.label 的大小,并将图像的尺寸与常量@987654326 一起传递给它必须是标签的尺寸@。

完整程序

这是完整的程序:

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

演示

Nota Bene

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

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

演示

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

【讨论】:

    猜你喜欢
    • 2018-11-23
    • 2021-05-13
    • 1970-01-01
    • 1970-01-01
    • 2020-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多