Fur*_*mer 1 python drag-and-drop tkinter customtkinter
我最近决定开始学习 Python,在做几个小项目作为实践方法时,我发现了这个customtkinter库 ( https://github.com/TomSchimansky/CustomTkinter ),可以使用 Python 进行更现代的 GUI 开发。
我想做的事情要么需要文件的拖放组件,要么需要文件选择器对话框,这似乎在带有模块的原始库中有所存在,但似乎在文档中没有直接提及库包装器。tkintertkinterdnd2customtkinter
有谁知道如何专门使用拖放文件customtkinter?
如果没有 的直接包装器customtkinter,有没有办法将 的样式应用customtkinter到tkinderdnd2模块?当像这样使用它时,显然它只是使用默认tkinter样式:
from tkinter import TOP, Entry, Label, StringVar
from tkinterdnd2 import *
def get_path(event):
pathLabel.configure(text = event.data)
root = TkinterDnD.Tk()
root.geometry("350x100")
root.title("Get file path")
nameVar = StringVar()
entryWidget = Entry(root)
entryWidget.pack(side=TOP, padx=5, pady=5)
pathLabel = Label(root, text="Drag and drop file in the entry box")
pathLabel.pack(side=TOP)
entryWidget.drop_target_register(DND_ALL)
entryWidget.dnd_bind("<<Drop>>", get_path)
root.mainloop()
Run Code Online (Sandbox Code Playgroud)
如果您查看以下源代码TkinterDnD:
class Tk(tkinter.Tk, DnDWrapper):
'''Creates a new instance of a tkinter.Tk() window; all methods of the
DnDWrapper class apply to this window and all its descendants.'''
def __init__(self, *args, **kw):
tkinter.Tk.__init__(self, *args, **kw)
self.TkdndVersion = _require(self)
Run Code Online (Sandbox Code Playgroud)
您可以使用customtkinter.CTk以下命令创建另一个自定义类tkinter.Tk:
from tkinter import StringVar, TOP
from tkinterdnd2 import TkinterDnD, DND_ALL
import customtkinter as ctk
class Tk(ctk.CTk, TkinterDnD.DnDWrapper):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.TkdndVersion = TkinterDnD._require(self)
ctk.set_appearance_mode("dark")
#ctk.set_default_color_theme("blue")
def get_path(event):
pathLabel.configure(text = event.data)
#root = TkinterDnD.Tk()
root = Tk()
root.geometry("350x100")
root.title("Get file path")
nameVar = StringVar()
entryWidget = ctk.CTkEntry(root)
entryWidget.pack(side=TOP, padx=5, pady=5)
pathLabel = ctk.CTkLabel(root, text="Drag and drop file in the entry box")
pathLabel.pack(side=TOP)
entryWidget.drop_target_register(DND_ALL)
entryWidget.dnd_bind("<<Drop>>", get_path)
root.mainloop()
Run Code Online (Sandbox Code Playgroud)
结果: