Python 在tkinter窗口中显示打开文件的位置名称
非常简单,不要对代码的大小印象深刻 我想做一件非常简单的事情(好吧,不是为我,因为我在这里寻求帮助)将打开文件的位置放在屏幕上的红方块中:Python 在tkinter窗口中显示打开文件的位置名称,python,tkinter,Python,Tkinter,非常简单,不要对代码的大小印象深刻 我想做一件非常简单的事情(好吧,不是为我,因为我在这里寻求帮助)将打开文件的位置放在屏幕上的红方块中: 将tkinter作为tk导入 从tkinter.filedialog导入askopenfilename 从tkinter导入消息框 def OpenFile_AntiDuplicate(): 全局反复制文件 mainframe=tk.Frame(bg='#1c2028') antiduplicate_file=askopenfilename(initia
将tkinter作为tk导入
从tkinter.filedialog导入askopenfilename
从tkinter导入消息框
def OpenFile_AntiDuplicate():
全局反复制文件
mainframe=tk.Frame(bg='#1c2028')
antiduplicate_file=askopenfilename(initialdir=“/”,
文件类型=((“文本文件”、“*.txt”)、(“所有文件”、“*.txt”),
title=“打开文本文件”
)
fichier_dir=tk.Label(大型机,text=antiduplicate_file).pack()
尝试:
打开(反复制_文件,'r')作为UseFile:
打印(反复制文件)
除:
打印(“不存在的文件”)
def RUN_AntiDuplicate():
尝试:
打开(反复制_文件,'r')作为UseFile:
打印(反复制文件)
除:
error1=tk.messagebox.bathror(“错误”,“不存在文件!”)
#----------------------------------------------------------
类悬停按钮(tk.Button):
def _初始功率(自功率、主功率,**kw):
tk.按钮。\uuuuu初始化(自,主=主,**kw)
self.defaultBackground=self[“background”]
self.bind(“,self.on_输入)
self.bind(“,self.on_-leave”)
def on_进入(自我,e):
self['background']=self['activebackground']
休假时的def(自我,e):
self['background']=self.defaultBackground
#----------------------------------------------------------
def Anti_Duplicate():
mainframe=tk.Frame(bg='#1c2028')
grid(行=0,列=0,sticky='nsew')
bouton_1=悬停按钮(大型机,字体=(“Arial”,10),text=“后退”,
背景='#000000',前景='白色',边框宽度=2,
activebackground='#202124',activeforeground='#CF3411',
relief='ridge',command=mainframe.destroy)
bouton_1.地点(x=520,y=300)
打开按钮=悬停按钮(大型机,字体=(“Arial”,10),text=“打开文件..”,
背景='#000000',前景='白色',边框宽度=2,
activebackground='#202124',activeforeground='#1195cf',
relief='ridge',command=OpenFile\u反复制)
打开按钮。放置(x=284.3,y=200,anchor='n')
运行按钮=悬停按钮(大型机,字体=(“Arial”,20),text=“运行”,
背景='#000000',前景='白色',边框宽度=2,
activebackground='#202124',activeforeground='#11CF6D',
relief='ridge',command=RUN\u反复制)
运行按钮。放置(x=50,y=330,anchor='s')
bouton_2=tk.按钮(主机,字体=(“Arial”,10),
text=“此工具的目的是从文本文件中删除重复的行。”,
背景='#202124',前景='#1195cf',边框宽度=2,
活动背景='#202124',活动前景='#1195cf',浮雕='凹陷')
bouton_2.place(relx=0.5,y=50,anchor=n')
bouton_1=tk.按钮(大型机,字体=(“Arial”,15),text=“防复制”,
背景='#202124',前景='#1195cf',边框宽度=2,
活动背景='#202124',活动前景='#1195cf',浮雕='凹陷')
bouton_1.pack(side=“top”,padx=5,pady=5,ipadx=30,anchor=“n”)
#----------------------------------------------------------
def主菜单():
root=tk.tk()
screenn\u x=int(root.winfo\u screenwidth())
root.config(后台='#1c2028')
screenn_y=int(root.winfo_screenheight())
根目录标题(“ComboKit v0.0.1”)
root.minsize(570340)
根目录。可调整大小(0,0)
窗口x=570
窗台y=340
possX=(屏幕n\u x//2)-(窗口x//2)
波西=(屏幕n\u y//2)-(窗口n\u y//2)
geoo=“{}x{}+{}+{}”。格式(windowssux,windowssuy,possX,possY)
根几何(geoo)
rowconfigure(0,权重=1)
root.columnconfigure(0,权重=1)
mainframe=tk.Frame(根,bg='#1c2028')
mainframe.grid(行=0,列=0,粘性=n')
main\u fusion\u bouton=HoverButton(大型机,字体=(“Arial”,15),text=“fusion”,
背景='#000000',前景='白色',边框宽度=2,
activebackground='#202124',activeforeground='#1195cf',
卸压(='ridge',命令=None)
主熔合块(side=“left”,padx=5,pady=5,ipadx=10,anchor=“n”)
main\u antiduplicate\u bouton=HoverButton(大型机,字体=(“Arial”,15),text=“防复制”,
背景='#000000',前景='白色',边框宽度=2,
activebackground='#202124',activeforeground='#1195cf',
relief='ridge',command=Anti_Duplicate)
主反耦合bouton.pack(side=“left”,padx=5,pady=5,ipadx=30)
main_split_button=悬停按钮(大型机,字体=(“Arial”,15),text=“split”,
背景='#000000',前景='白色',边框宽度=2,
activebackground='#202124',activeforeground='#1195cf',
卸压(='ridge',命令=None)
主拆分按钮组件(side=“left”,padx=5,pady=5,ipadx=19,anchor=“n”)
root.mainloop()
主菜单()
下面是我的代码,允许您使用3种工具:
“拆分”/“防重复”/“合并”
目前我正在处理“反复制”问题,所以它在这个框架()上,文本应该显示在这个框架上
我已经做了所有的事情,甚至打开文件浏览器的按钮,但目前文件的位置只显示在cmd中
多谢各位 文件的位置不会显示,因为您创建了一个新的框架来保存标签
fichier\u dir
内部OpenFile\u AntiDuplicate()
,并且您没有调用任何布局乐趣
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()
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))
...
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
...