Display a msg in Tkinter GUI when a function has completed - python

I have a simple Tkinter GUI with a button that runs a function to import a file from a local directory..
import tkinter as tk
from tkinter import filedialog
from bs4 import BeautifulSoup
root = tk.Tk()
root.geometry("1000x800")
def import_from_file():
imported_file_name = filedialog.askopenfilename(filetypes = [("All Files" , ".html")])
html_string = open(imported_file_name, 'r')
import_button = tk.Button(root, text= "Import html from file", command = import_from_file)
import_button.place(x=10, y=0)
root.mainloop()
My question is, what is the best way to display a message in the Tkinter GUI eg "Finshed" when that function has run, without closing the GUI?
If i use:
def import_from_file():
imported_file_name = filedialog.askopenfilename(filetypes = [("All Files" , ".html")])
html_string = open(imported_file_name, 'r')
print('Finished')
This will only display Finished in the terminal when the Tkinter GUI is closed if it runs successfully.

Related

Tkinter window and script freeze when filedialog is used

Im developing a audio player for python. Unfortunately, it appears as if after a modification, the file dialog portion of the script ceased working. The code below is a minimum reproducible example of the offending bit.
# -----------------------Setup-----------------------
# Imports
from tkinter import *
from tkinter.ttk import *
from tkinter import filedialog
import simpleaudio
import subprocess
# GUI
root = Tk()
root.title("CupPlayer")
menubar = Menu(root)
file = Menu(menubar, tearoff = 0)
root.config(menu = menubar)
# ----------------------Functions---------------------
def file_selector():
filename = filedialog.askopenfilename()
root.update()
if filename.endswith('.mp3'):
subprocess.call(['ffmpeg', '-i', filename,
'audio.wav'])
btn = Button(root, text = 'search file !', bd = '5',
command = file_selector)
mainloop()
Hey #RandomInternetPerson first of all dont use subprocess.call inside a ui loop at all its make freeze and crashing the ui it will wait until subprocess process is open so change it to subprocess.Popen and check if it fixed

How to get a user-selected file and feed it to a python program in Tkinter

I am currently messing about with Tkinter creating an interface for a project I created. The program takes in a bunch of file paths as inputs for it to run. I'm trying to create a tkinter interface where I can upload the 4 files I need or at least somehow get the filepaths and the. feed those to the program. Here is what I have:
import sys
import os
import comparatorclass
from tkinter import *
from tkinter.ttk import *
from tkinter.filedialog import askopenfile
root=Tk()
root.geometry('1000x1000')
def open_file():
file = askopenfile(mode ='r', filetypes =[('Python Files', '*.py')])
if file is not None:
content = file.read()
print(content)
def run_comparator():
comparatorclass.main()
button2 = Button(root, text ='Open', command = lambda:open_file())
button2.pack(side = TOP, pady = 10)
button1 = Button(root,text="hello",command= run_comparator)
button1.pack()
root.mainloop()
as you can see, I have two buttons. The issue I'm having is how to connect my openfile function to my run_comparator function such that the 4 files I need to open are passed on to the run_comparator

How I used tkinter button to Import files

I would like to run a python program and when clicking a button in a (tkinter) window become import files form any direction and store it to another direction and in database .
import tkinter as tk
from tkinter import filedialog
def UploadAction(event=None):
filename = filedialog.askopenfilenames()
print('Selected:', filename)
root = tk.Tk()
button = tk.Button(root, text='import files', command=UploadAction)
button.grid(row=1,column=1,padx=10,pady=10)
root.mainloop()

How to open a exe file using python Tkinter

I am trying to open a exe file from C drive by clicking a button from my GUI and up till now i am unable to select the specific file. Can i know if there is any function to directly open the file in Tkinter. Currently,tkFileDialog.askdirectory only direct me to the FILES.
import Tkinter
import tkMessageBox
import tkFileDialog
import os
import subprocess
top = Tkinter.Tk()
def run():
File = tkFileDialog.askdirectory()
os.system(File)
b = Tkinter.Button(top, text = 'DAQoutput', command= run)
b.pack()
top.mainloop()
Directly using Tkinter? No there is no command for it but you don't need one. Tkinter is a GUI toolkit and that's all it does.
Instead use os.system() to launch your exe.
Your script should look something like this:
import Tkinter
import tkMessageBox
import tkFileDialog
import os
top = Tkinter.Tk()
def run(self):
File = tkFileDialog.askdirectory()
os.system(File)
b = Tkinter.Button(top, text = 'DAQoutput', command= self.run)
b.pack()
top.mainloop()

Open File Dialog freezes after tkinter askopenfilename method is called

I'm trying to simply get a file name from the user by tkinter.filedialog.askopenfilename(). The function returns fine and the code below displays the file name okay but the dialog window doesn't close immediately after hitting 'open' or 'cancel', it freezes. I'm using python 3.3.3 or OSX 10.9.1 and tcl/tK 8.5.9.
from tkinter import *
from tkinter.messagebox import *
from tkinter.filedialog import *
top = Tk()
top.withdraw()
file_name = filedialog.askopenfilename()
print (file_name)
Adding root.update() after filedialog.askopenfilename() gets the open file dialog to close after the file is chosen.
root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename()
root.update()
Refer to: Tkinter askopenfilename() won't close
A work around that I used for this is to "withdraw" the tkinter window AFTER selecting the file from file explorer. Here is the code snippet I used -
import tkinter
from tkinter import filedialog
def selectCustomerFileTK():
root = tkinter.Tk()
root.wm_attributes('-topmost', 1)
filename = filedialog.askopenfilename()
root.withdraw()
return filename
getfile = selectCustomerFileTK()
It opens a tkinter window while you are selecting the file, but the moment you select the file and press "Open", the tkinter window and file explorer both close because of the "root.withdraw()" command after.
You don't need to specify module name in
file_name = filedialog.askopenfilename()
Try
file_name = askopenfilename()
instead

Categories

Resources