【问题标题】:Tkinter: Problem when adding photos to the buttonsTkinter:将照片添加到按钮时出现问题
【发布时间】:2021-07-18 10:27:35
【问题描述】:

大家好,我的问题是当我向按钮添加图像时,它会显示一个空框,以及如何在每一帧中放置背景照片?我是 OOP 的新手。

    import tkinter as tk
    from tkinter import ttk 
    from PIL import ImageTk, Image
    
    LARGE_FONT = ("Calibri", 12)

    class Gui(tk.Tk):
        def __init__(self, *agrs, **kwargs):
            tk.Tk.__init__(self, *agrs, **kwargs) #initialize tkinter
    
            tk.Tk.title(self, "COVID-19 Tracker") #Front Window Title
            tk.Tk.geometry(self, '400x600') #pixels 
            tk.Tk.iconbitmap(self, 'Images/covid19-logo.ico')
    
            container = tk.Frame(self)
            container.pack(side="top", fill="both", expand = True)
            container.grid_rowconfigure(0, weight=1)
            container.grid_columnconfigure(0, weight=1)
    
            self.frames = {}
            #Loop to access different frames
            for x in (StartPage, PageOneUser, PageOneAdmin, PageOneSignUp, PageTwoSignUp):
                frame = x(container, self)
                self.frames[x] = frame
                frame.grid(row=0, column=0, sticky="nsew")
    
            self.show_frame(StartPage)
    
        def show_frame(self, cont):
            frame = self.frames[cont]
            frame.tkraise()

class StartPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        #Icons 
        user_image = tk.PhotoImage(file='Images/user_button.png')
        show_image = tk.Label(self, image=user_image)
        admin_image = tk.PhotoImage(file='Images/admin_button.png')
        signup_image = tk.PhotoImage(file='Images/signup_button.png')
        about_image = tk.PhotoImage(file='Images/about_button.png')


        # Background Photo
       
        #Widgets
        user_button = tk.Button(self, text="USER", image=user_image,
            command=lambda: controller.show_frame(PageOneUser))
        user_button.place(x=115, y=270)

        admin_button = tk.Button(self, text="ADMIN", 
            command=lambda: controller.show_frame(PageOneAdmin))
        admin_button.place(x=93, y=355)

        signup_button = tk.Button(self, text="SIGN", 
            command=lambda: controller.show_frame(PageOneSignUp))
        signup_button.place(x=129, y=440)

        about_button = tk.Button(self, text="ABOUT", 
            command=lambda: controller.show_frame(OpenAboutWindow))
        about_button.place(x=300, y=550)

# User Log In Form 
class PageOneUser(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        #Widgets
        login_button = tk.Button(self, text="LOG IN", 
            command=lambda: controller.show_frame(UserDashboard))
        login_button.place(x=140, y=320)

        back_button = tk.Button(self, text="BACK", 
            command=lambda: controller.show_frame(StartPage))
        back_button.place(x=300, y=550)

        #Entry 
        # textvariable = get_varUsername
        username_entry = tk.Entry(self, width=30).place(x=109, y=210)

        password_entry = tk.Entry(self, width=30).place(x=109, y=280)

# Admin Log In Form
class PageOneAdmin(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        #Widgets
        login_button = tk.Button(self, text="LOG IN", 
            command=lambda: controller.show_frame(UserDashboard))
        login_button.place(x=140, y=320)

        back_button = tk.Button(self, text="BACK", 
            command=lambda: controller.show_frame(StartPage))
        back_button.place(x=300, y=550)

        #Entry 
        username_entry =tk.Entry(self, width=30).place(x=109, y=210)

        password_entry = tk.Entry(self, width=30, show = '*').place(x=109, y=280)


# Personal Details and Log In Details
class PageOneSignUp(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        #Widget
        next_button = tk.Button(self, text="NEXT", 
            command=lambda: controller.show_frame(PageTwoSignUp))
        next_button.place(x=300, y=550)

        back_button = tk.Button(self, text="BACK", 
            command=lambda: controller.show_frame(StartPage))
        back_button.place(x=30, y=550)

#Contact Tracing Form
class PageTwoSignUp(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        #Widgets
        signup_button = tk.Button(self, text="SIGN UP", 
            command=lambda: controller.show_frame(StartPage))
        signup_button.place(x=260, y=540)

        back_button = tk.Button(self, text="BACK", 
            command=lambda: controller.show_frame(PageOneSignUp))
        back_button.place(x=30, y=550)

class OpenAboutWindow(tk.Frame):
    pass

class UserDashboard(tk.Frame):
    pass

app = Gui()
app.mainloop()

【问题讨论】:

标签: python oop tkinter


【解决方案1】:

尝试这样使用

这个compound=RIGHT 帮助您确定文本和图标的位置

compound=RIGHT / LEFT

使用左或右

这是你的代码

    user_image = tk.PhotoImage(file='Images/user_button.png')
    user_button = tk.Button(self, text="USER", image=user_image,
        command=lambda: controller.show_frame(PageOneUser))
    user_button.place(x=115, y=270)

这样改变

    image_icon = tk.PhotoImage(file='Images/user_button.png')
    user_image = image_icon .subsample(3, 3)

    user_button = tk.Button(self, text="USER", image=user_image,compound=RIGHT,
        command=lambda: controller.show_frame(PageOneUser))
     user_button.place(x=115, y=270)

希望这段代码对你有所帮助

输出:

【讨论】:

  • 您好先生,它不起作用,它显示相同的输出但大小不同,并且按钮不可点击。
  • 请正确检查您的代码在哪里遗漏了什么以及哪个按钮不可点击?
  • 你知道你用错了class吗? class window1 中的代码即使没有调用 window1() 也会被执行。
  • 这是用户如何在 @acw1668 的 Button 中添加图像的唯一示例
猜你喜欢
  • 1970-01-01
  • 2011-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-26
  • 1970-01-01
  • 2021-12-27
  • 1970-01-01
相关资源
最近更新 更多