我目前正在尝试使用 tkinter 为自己创建一个桌面文件转换器应用程序。到目前为止,它有一个拖放区域和一个按钮,您可以按下它来从文件资源管理器中选择文件。但是,我无法弄清楚如何正确定位小部件,使它们彼此重叠。我想要它,所以拖放框不在屏幕的左侧,在框的中间我想要一个文本小部件,上面写着“拖放文件,或选择它们”,下面有一个按钮小部件它允许他们根据需要从文件管理器中进行选择。
import tkinter as tk
import tkinter.filedialog
from TkinterDnD2 import DND_FILES, TkinterDnD
from conversion import *
#global variables
path_to_file = " "
file_type = " "
compatable_converstion = []
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.master.title("File Converter")
self.master.minsize(1000,600)
self.master.maxsize(1200,800)
self.pack()
self.create_widgets()
def create_widgets(self):
#Drag and drop files area
self.drop_box = tk.Listbox(root, selectmode=tk.SINGLE, background="#99ff99")
self.drop_box.pack(ipadx=170)
self.drop_box.pack(ipady=120)
self.drop_box.pack(side="left")
self.drop_box.drop_target_register(DND_FILES)
self.drop_box.dnd_bind("<<Drop>>", open_dropped_file)
#Select file button
self.select_file = tk.Button(self)
self.select_file["text"] = "Select File"
self.select_file["command"] = self.open_selected_file
self.select_file.place(relx=1.0, rely=1.0, anchor="se")
#Instructional Text
sentence = "Drag and drop or select your file"
self.instructions = tk.Text(root)
self.instructions.insert(tk.END, sentence)
self.instructions.place(relx=1.0, rely=1.0, anchor="se")
def open_selected_file(self):
path_to_file = tk.filedialog.askopenfilename(initialdir="/", title="Select A File", filetypes = (("jpeg files","*.jpg"),("all files","*.*")))
temp_str = " "
for chars in reversed(path_to_file):
if(chars == '.'):
break
temp_str += chars
file_type = temp_str[::-1]
compatable_converstion = retrieve_compatable_conversions(file_type)
def main():
global root
root = TkinterDnD.Tk()
app = Application(master=root)
app.mainloop()
if __name__ == "__main__":
main()
答案 0 :(得分:0)
您正在根窗口上创建小部件 self.drop_box
和 self.instructions
。它们应该在 self
上创建。
place()
几何管理器不会在小部件中保留空间,因此您必须使用 self
使小部件 (expand=True, fill='both'
) 尽可能大。我建议对任何不太简单的设计使用 grid()
几何管理器。
小部件将按照它们的放置顺序堆叠,这意味着按钮将隐藏在文本小部件下方。只需更改它们的放置顺序即可。
至于在ipadx
函数中使用ipady
和pack()
,看看Why does the 'ipady' option in the tkinter.grid() method only add space below a widget?
还有;您不需要将 root 设为全局变量,因为应用程序将其识别为 self.master
。
这是一个例子,不是整个程序,而是我提到的部分:
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.master.title("File Converter")
self.master.minsize(1000,600)
self.master.maxsize(1200,800)
self.pack(expand=True, fill='both') # Fill entire root window
self.create_widgets()
def create_widgets(self):
#Drag and drop files area
self.drop_box = tk.Listbox(self, selectmode=tk.SINGLE, background="#99ff99")
self.drop_box.pack(side="left", ipadx=170, ipady=120)
self.drop_box.drop_target_register(DND_FILES)
self.drop_box.dnd_bind("<<Drop>>", open_dropped_file)
#Instructional Text
sentence = "Drag and drop or select your file"
self.instructions = tk.Text(self)
self.instructions.insert(tk.END, sentence)
self.instructions.place(relx=1.0, rely=1.0, anchor="se")
#Select file button
self.select_file = tk.Button(self, text="Select File",
command=self.open_selected_file)
self.select_file.place(relx=1.0, rely=1.0, anchor="se")
这应该可以让您解决大部分问题。