简体   繁体   English

我可以在哪里使用 grid() 方法将图像存储在 Tkinter 上?

[英]Where can I store an image on Tkinter using grid() method?

I am creating a login system with Tkinter and the grid() method but I have no idea where I can put the image.我正在使用 Tkinter 和 grid() 方法创建一个登录系统,但我不知道可以将图像放在哪里。 As i did not use classes and functions, it was pretty easy to embed the path for the image ( img = PhotoImage(file = r"C:\\Users\\admin\\Desktop\\Foto\\Haken.png") img1 = img.subsample(10,10) , but, since I am new in Python, I don´t really know where to put the path in this code when the code is more organized. Here is what I tried:由于我没有使用类和函数,因此嵌入图像的路径非常容易( 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()

But I get this error:但我得到这个错误:

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]

Have you got any suggestion?你有什么建议吗? I would like to put the image on the far right column.我想把图像放在最右边的栏中。

If the image is part of the Login_system , then it is better put it inside the class.如果图像是Login_system的一部分,那么最好将其放在 class 中。 Also you forget to specify the parent of widgets inside the class, so the widgets will be children of root instead.此外,您忘记在 class 中指定小部件的父级,因此小部件将改为root的子级。

Also avoid importing modules like below:还要避免导入如下模块:

from tkinter import *
from tkinter.ttk import *

In this case, you cannot use some of the widgets from tkinter because they are override by those from ttk .在这种情况下,您不能使用 tkinter 中的一些小部件,因为它们被来自tkinter的小部件ttk

Below is a modified sample based on your code:以下是根据您的代码修改后的示例:

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

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

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