繁体   English   中英

tkinter按钮不显示

[英]tkinter buttons not displaying

当我执行脚本时,背景图像可以正常工作,它将匹配窗口的大小,但是,我无法显示按钮(它们还没有功能)。 我对python相当新,所以我不确定我是否使用按钮作为事件是一个好主意。 任何帮助表示赞赏。

import turtle
import tkinter as tk
from tkinter import *
from PIL import Image, ImageTk

root = Tk()

taxi = (r"C:\directory\image.png")

class App(Frame):
    def __init__(self, master, Buttons=None):
        Frame.__init__(self, master, Buttons)
        self.columnconfigure(0,weight=1)
        self.rowconfigure(0,weight=1)
        self.original = Image.open(r"C:\directory\Layout.gif")
        self.image = ImageTk.PhotoImage(self.original)
        self.display = Canvas(self, bd=0, highlightthickness=0)
        self.display.create_image(500, 500, image=self.image, anchor=NW, tags="IMG")
        self.display.grid(row=0, column=0, sticky=W+E+N+S)
        self.pack(fill='both', expand=True)
        self.bind("<Configure>", self.resize)

    def resize(self, event):
        size = (event.width, event.height)
        resized = self.original.resize(size,Image.ANTIALIAS)
        self.image = ImageTk.PhotoImage(resized)
        self.display.delete("IMG")
        self.display.create_image(0, 0, image=self.image, anchor=NW, tags="IMG")

    def Buttons(self, event):
        self.Button1 = tk.Button(master = root, text = "Button1") #, command = forward).pack(side = tk.LEFT)
        self.Button1.grid(row=1, column=1)

        self.Button2 = tk.Button(master = root, text = "Button2") #, command = forward).pack(side = tk.LEFT)
        self.Button2.grid(row=2, column=1)

        self.Button3 = tk.Button(master = root, text = "Button3") #, command = forward).pack(side = tk.LEFT)
        self.Button3.grid(row=3, column=1)


app = App(root)
app.mainloop()

我在评论中添加了您的代码中的建议,并删除了一些不相关的行(针对该问题)。 现在按钮显示:

  1. 从Buttons方法中删除events参数。

  2. self.__init__ self.Buttons()添加到self.__init__

最小工作(已解决)示例。

from tkinter import *


class App(Frame):
    def __init__(self, master):
        Frame.__init__(self, master)
        self.columnconfigure(0, weight=1)
        self.rowconfigure(0, weight=1)
        self.pack(fill='both', expand=True)
        self.bind("<Configure>", self.resize)
        self.Buttons()  # <--------------------- IMPORTANT!

    def resize(self, event):
        size = (event.width, event.height)

    def Buttons(self):  # <--------------------- Remove the event argument
        self.Button1 = Button(master=self, text="OLD VINS")
        self.Button1.grid(row=1, column=1)

        self.Button2 = Button(master=self, text="QBAY")
        self.Button2.grid(row=2, column=1)

        self.Button3 = Button(master=self, text="HELP")
        self.Button3.grid(row=3, column=1)


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

暂无
暂无

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

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