簡體   English   中英

在 tkinter 窗口中顯示打開文件的位置名稱

[英]Display the location name of the open file in the tkinter window

很簡單,不要被代碼的大小留下深刻印象。

我想做一些非常簡單的事情(好吧,不適合我,因為我在這里尋求幫助)將打開文件的位置放在屏幕上的紅色方塊中:

屏幕


import tkinter as tk
from tkinter.filedialog import askopenfilename
from tkinter import messagebox

def OpenFile_AntiDuplicate():

    global antiduplicate_file
    mainframe = tk.Frame(bg='#1c2028')

    antiduplicate_file = askopenfilename(initialdir="/",
                        filetypes =(("Text file", "*.txt"),("All files","*.*")),
                        title = "Open text file"
                        )

    fichier_dir = tk.Label(mainframe, text=antiduplicate_file).pack()

    try:

        with open(antiduplicate_file,'r') as UseFile:
            print(antiduplicate_file)

    except:

        print("Non-existent file")


def RUN_AntiDuplicate():

    try:
        with open(antiduplicate_file,'r') as UseFile:
            print(antiduplicate_file)

    except:

        error1 = tk.messagebox.showerror("ERROR", "No files exist!")

#----------------------------------------------------------

class HoverButton(tk.Button):
    def __init__(self, master, **kw):
        tk.Button.__init__(self,master=master,**kw)
        self.defaultBackground = self["background"]
        self.bind("<Enter>", self.on_enter)
        self.bind("<Leave>", self.on_leave)

    def on_enter(self, e):
        self['background'] = self['activebackground']

    def on_leave(self, e):
        self['background'] = self.defaultBackground

#----------------------------------------------------------

def Anti_Duplicate():

   mainframe = tk.Frame(bg='#1c2028')
   mainframe.grid(row=0, column=0, sticky='nsew')

   bouton_1 = HoverButton(mainframe, font=("Arial", 10), text="Back",
                          background='#000000', fg='white', borderwidth=2,
                          activebackground='#202124', activeforeground='#CF3411',
                          relief='ridge', command=mainframe.destroy)

   bouton_1.place(x=520, y=300)

   open_button = HoverButton(mainframe, font=("Arial", 10), text="Open File..",
                             background='#000000', fg='white', borderwidth=2,
                             activebackground='#202124', activeforeground='#1195cf',
                             relief='ridge', command = OpenFile_AntiDuplicate)
   open_button.place(x=284.3, y=200, anchor='n')

   run_button = HoverButton(mainframe, font=("Arial", 20), text="RUN",
                             background='#000000', fg='white', borderwidth=2,
                             activebackground='#202124', activeforeground='#11CF6D',
                             relief='ridge', command = RUN_AntiDuplicate)
   run_button.place(x=50, y=330, anchor='s')


   bouton_2 = tk.Button(mainframe, font=("Arial", 10),
                        text="The purpose of this tool is to remove duplicate lines from a text file.",
                        background='#202124', fg='#1195cf', borderwidth=2,
                        activebackground= '#202124', activeforeground='#1195cf', relief='sunken')
   bouton_2.place(relx=.5, y=50, anchor='n')

   bouton_1 = tk.Button(mainframe, font=("Arial", 15), text="Anti-Duplicate",
                        background='#202124', fg='#1195cf',  borderwidth=2,
                        activebackground='#202124', activeforeground='#1195cf', relief='sunken')
   bouton_1.pack(side= "top", padx= 5, pady=5, ipadx= 30, anchor="n")


#----------------------------------------------------------

def main_menu():

   root = tk.Tk()
   screenn_x = int(root.winfo_screenwidth())
   root.config(background='#1c2028')
   screenn_y = int(root.winfo_screenheight()) 
   root.title("ComboKit v0.0.1")
   root.minsize(570, 340)
   root.resizable(0,0)

   windowss_x = 570
   windowss_y = 340

   possX = (screenn_x // 2) - (windowss_x // 2)
   possY = (screenn_y // 2) - (windowss_y // 2)

   geoo = "{}x{}+{}+{}".format(windowss_x, windowss_y, possX, possY)
   root.geometry(geoo)

   root.rowconfigure(0, weight=1)
   root.columnconfigure(0, weight=1)

   mainframe = tk.Frame(root, bg='#1c2028')
   mainframe.grid(row=0, column=0, sticky='n')

   main_fusion_bouton = HoverButton(mainframe, font=("Arial", 15), text="Fusion",
                          background='#000000', fg='white', borderwidth=2,
                          activebackground='#202124', activeforeground='#1195cf',
                          relief='ridge', command=None)
   main_fusion_bouton.pack(side= "left", padx= 5, pady=5, ipadx= 10, anchor="n")

   main_antiduplicate_bouton = HoverButton(mainframe, font=("Arial", 15), text="Anti-Duplicate",
                          background='#000000', fg='white', borderwidth=2,
                          activebackground='#202124', activeforeground='#1195cf',
                          relief='ridge', command=Anti_Duplicate)

   main_antiduplicate_bouton.pack(side= "left", padx= 5, pady=5, ipadx= 30)

   main_split_button = HoverButton(mainframe, font=("Arial", 15), text="Split",
                          background='#000000', fg='white', borderwidth=2,
                          activebackground='#202124', activeforeground='#1195cf',
                          relief='ridge', command=None)

   main_split_button.pack(side= "left", padx= 5, pady=5, ipadx= 19, anchor="n")

   root.mainloop()

main_menu()

所以這是我的代碼允許您使用 3 個工具:

“拆分”/“反復制”/“合並”

目前我正在研究“反重復”,所以它在這個 Frame() 上應該顯示文本。

我已經完成了所有操作,甚至是打開文件資源管理器的按鈕,但目前文件的位置僅顯示在 cmd 中。

非常感謝!

文件的位置不會顯示,因為您在OpenFile_AntiDuplicate()創建了一個新框架來保存標簽fichier_dir並且您沒有在框架上調用任何布局函數,因此不會顯示框架。

最好在Anti_Duplicate()創建標簽fichier_dir並將其傳遞給OpenFile_AntiDuplicate()函數:

def Anti_Duplicate():
   ...    
   fichier_dir = tk.Label(mainframe, bg='#1c2028', fg='white')
   fichier_dir.place(relx=0.5, y=170, anchor='n')

   open_button = HoverButton(mainframe, font=("Arial", 10), text="Open File..",
                             background='#000000', fg='white', borderwidth=2,
                             activebackground='#202124', activeforeground='#1195cf',
                             relief='ridge', command = lambda: OpenFile_AntiDuplicate(fichier_dir))
   ...

並更新OpenFile_AntiDuplicate(...)

def OpenFile_AntiDuplicate(fichier_dir):
    global antiduplicate_file

    antiduplicate_file = askopenfilename(initialdir="/",
                         filetypes =(("Text file", "*.txt"),("All files","*.*")),
                         title = "Open text file"
                         )

    fichier_dir['text'] = antiduplicate_file
    ... 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM