python-tkinter按钮不显示
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python-tkinter按钮不显示,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2387字,纯文字阅读大概需要4分钟。
内容图文
![python-tkinter按钮不显示](/upload/InfoBanner/zyjiaocheng/650/37f01b1fc78f444ab9053f01644e9026.jpg)
当我执行脚本时,背景图像可以正常工作,它将与窗口的大小匹配,但是,我无法显示按钮(它们尚无功能).我对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()
解决方法:
我在注释中为您的代码添加了建议,并删除了一些与问题无关的行.现在,按钮显示:
>从Buttons方法中删除events参数.
>将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()
内容总结
以上是互联网集市为您收集整理的python-tkinter按钮不显示全部内容,希望文章能够帮你解决python-tkinter按钮不显示所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。