我正在尝试向窗口添加三个按钮。我没有分类就尝试了,效果很好。但是,当我将其添加到课程中时,它只会显示一个空窗口。我究竟做错了什么?
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()
答案 0 :(得分:0)
initializeOutlineFrame()
代替initializeOutlineFrame
。initializeOutlineFrame(self, master)
中,将所有master
更改为self.master
。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)