【问题标题】:Tkinter Widget PlacementTkinter 小部件放置
【发布时间】:2021-03-02 17:54:26
【问题描述】:

我目前正在尝试使用 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()

以防我对它的布局方式的解释很糟糕,这是一张图片:

【问题讨论】:

    标签: python tkinter


    【解决方案1】:

    您正在根窗口上创建小部件self.drop_boxself.instructions。它们应该在self 上创建。

    place() 几何管理器不会在小部件中保留空间,因此您必须使用 expand=True, fill='both' 使小部件 (self) 尽可能大。对于任何不是很简单的设计,我建议使用grid() 几何管理器。

    小部件将按照它们的放置顺序堆叠,这意味着按钮将隐藏在文本小部件下方。只需更改它们的放置顺序即可。

    关于在pack()函数中使用ipadxipady,看看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")
    

    这应该可以让您解决大部分问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-13
      • 2019-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多