【问题标题】:Button not being displayed in window按钮未显示在窗口中
【发布时间】:2020-05-26 11:16:44
【问题描述】:

我正在尝试向一个窗口添加三个按钮。我在没有分类的情况下尝试过它,它工作正常。但是当我将它添加到一个类中时,它只显示一个空窗口。我究竟做错了什么?

PS:我还在学习python中的绳索。

from tkinter import filedialog,Button,Frame,Tk

class Textify:

    def __init__(self,master):
        self.master = master
        self.initializeOutlineFrame

    def initializeOutlineFrame(self):
        self.convertImageButton = Button(master,text="Convert Image",command=self.browseIMG,width=80,height=30)
        self.convertImageButton.pack()
        self.convertPdfButton = Button(master,text="Convert pdf",command=self.browsePDF)
        self.convertPdfButton.pack()
        self.batchConvertButton = Button(master,text="Convert multiple files",command=self.browseAllFiles)
        self.batchConvertButton.pack()

    def browsePDF(self):
        filename = filedialog.askopenfilename(initialdir = "/",
        title = "Select a File",filetypes = (("pdf files","*.pdf*"),("all files", "*.*")))

    def browseIMG(self):
        filename = filedialog.askopenfilename(initialdir = "/",
        title = "Select a File",filetypes = (("jpg files","*.JPG*"),("png files","*.PNG*")))

    def browseAllFiles(self):
        filename = filedialog.askopenfilename(initialdir = "/",
        title = "Select files",filetypes = (("jpg files","*.JPG*"),("png files","*.PNG*"),("pdf files","*.pdf*")))


window = Tk()
window.title("Textify")
window.geometry("450x450")
app = Textify(window)
window.mainloop()

【问题讨论】:

  • self.initializeOutlineFrame 什么都不做;您只是提到了方法的名称,而不是调用它。试试self.initializeOutlineFrame()
  • 试过了。还是什么都没做
  • 还需要将函数initializeOutlineFrame中的master改为self.master

标签: python tkinter


【解决方案1】:
  1. 使用initializeOutlineFrame() 而不是initializeOutlineFrame
  2. def initializeOutlineFrame(self, master) 中,将所有master 更改为self.master
  3. 小心! width=80,height=30 没有以像素为单位定义大小,而是以字符为单位。所以在这个阶段,如果你运行代码,你会看到一个巨大的按钮,而不是其他按钮。删除或更改值,例如 width=17,height=3

[可选] 您可以使用网格按钮代替包装来增加周围的空间。它看起来更好:

def initializeOutlineFrame(self):

    self.convertImageButton = Button(self.master, text="Convert Image", \
    command=self.browseIMG, width=17, height=3)
    self.convertImageButton.grid(column=0, row=0, padx=10, pady=10)

    self.convertPdfButton = Button(self.master, text="Convert pdf", \
    command=self.browsePDF, width=17, height=3)
    self.convertPdfButton.grid(column=1, row=0, padx=10, pady=10)

    self.batchConvertButton = Button(self.master, text="Convert multiple files", \
    command=self.browseAllFiles, width=17, height=3)
    self.batchConvertButton.grid(column=2, row=0, padx=10, pady=10)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多