【问题标题】:Where can I store an image on Tkinter using grid() method?我可以在哪里使用 grid() 方法在 Tkinter 上存储图像?
【发布时间】:2020-08-18 08:10:08
【问题描述】:

我正在使用 Tkinter 和 grid() 方法创建一个登录系统,但我不知道我可以将图像放在哪里。由于我没有使用类和函数,因此很容易嵌入图像的路径(img = PhotoImage(file = r"C:\\Users\\admin\\Desktop\\Foto\\Haken.png")img1 = img.subsample(10,10),但是,由于我是 Python 新手,我真的不知道在哪里放置路径代码更有条理时的代码。这是我尝试过的:

from tkinter import *
from tkinter.ttk import *


class Login_system(Frame):
    
    def __init__(self, parent):
        Frame.__init__(self, parent)
        self.parent = parent 
        self.initUI()


    def initUI(self):
        
        self.parent.title("Login System Prova")

        Label(text = "Surname").grid(row=0, column=0, sticky=W, pady=2)
        Label(text = "Your Password").grid(row=1, column=0, sticky=W, pady=2)
        Label(text = "Your E-Mail").grid(row=2, column=0,sticky=W, pady=2)
        Entry().grid(row = 0, column=1, pady=2)
        Entry().grid(row = 1, column=1, pady=2)
        Entry().grid(row = 2, column=1, pady=2)
        Entry().grid(row = 3, column=1, pady=2)
        Checkbutton(text = "Keep me on-line").grid(row = 4, sticky = W, columnspan= 1)
        
        


def main():
    root = Tk()
    root.geometry("200x150+400+300")
    root.resizable(True, True)
    global image
    image = Frame.PhotoImage(file = r"C:\\Users\\admin\\Desktop\\Foto\\Haken.png")
    app = Login_system(root)
    root.mainloop()


if __name__ == "__main__":
    main()

但我收到此错误:

Traceback (most recent call last):
  File "C:\Users\admin\Desktop\Python\GUI APP\login_system_new.py", line 40, in <module>
    main()
  File "C:\Users\admin\Desktop\Python\GUI APP\login_system_new.py", line 34, in main
    image = Frame.PhotoImage(file = r"C:\\Users\\admin\\Desktop\\Foto_Marco\\Haken.png")
AttributeError: type object 'Frame' has no attribute 'PhotoImage'
[Finished in 0.5s]

你有什么建议吗?我想把图片放在最右边的一列。

【问题讨论】:

  • 删除Frame.,只删除PhotoImage(file=...)
  • 我试过了。它没有给我任何错误,但是当窗口出现时,根本没有图像。
  • 您的代码没有将图像放入窗口。您需要 LabelCanvas 来显示图像。

标签: python image tkinter


【解决方案1】:

如果图像是Login_system 的一部分,那么最好将它放在类中。此外,您忘记在类中指定小部件的父级,因此小部件将是 root 的子级。

同时避免导入如下模块:

from tkinter import *
from tkinter.ttk import *

在这种情况下,您不能使用来自tkinter 的一些小部件,因为它们会被来自ttk 的小部件覆盖。

以下是根据您的代码修改后的示例:

import tkinter as tk
from tkinter import ttk

class Login_system(ttk.Frame):
    def __init__(self, parent):
        ttk.Frame.__init__(self, parent)
        self.initUI()

    def initUI(self):
        self.master.title("Login System Prova")

        ttk.Label(self, text="Surname").grid(row=0, column=0, sticky=tk.W, pady=2)
        ttk.Label(self, text="Your Password").grid(row=1, column=0, sticky=tk.W, pady=2)
        ttk.Label(self, text="Your E-Mail").grid(row=2, column=0, sticky=tk.W, pady=2)
        ttk.Entry(self).grid(row=0, column=1, pady=2)
        ttk.Entry(self).grid(row=1, column=1, pady=2)
        ttk.Entry(self).grid(row=2, column=1, pady=2)
        ttk.Entry(self).grid(row=3, column=1, pady=2)
        ttk.Checkbutton(self, text="Keep me on-line").grid(row=4, sticky=tk.W, columnspan=2)

        self.image = tk.PhotoImage(file=r"C:\\Users\\admin\\Desktop\\Foto\\Haken.png").subsample(10,10)
        ttk.Label(self, image=self.image).grid(row=0, column=2, rowspan=5, padx=(20,0))

def main():
    root = tk.Tk()
    #root.geometry("200x150+400+300")
    #root.resizable(True, True)
    app = Login_system(root)
    app.pack(fill='both', expand=1, padx=10, pady=10)
    root.mainloop()

if __name__ == "__main__":
    main()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-27
    • 1970-01-01
    相关资源
    最近更新 更多