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()
Related
I want to create a button that opens the special directory like "C:\Users\", But as I searched, I did not find any code except the following code:
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
gui = Tk()
gui.geometry("100x100")
def getFolderPath():
filedialog.askdirectory()
btnFind = ttk.Button(gui, text="Open Folder",command=getFolderPath)
btnFind.grid(row=0,column=2)
gui.mainloop()
What is the correct way to solve this problem?
Use os.startfile("C:\\Users") to use the system file explorer to open the directory.
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
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
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.
I'm making a program that you use the askopenname file dialog to select a file, which I then want to save the directory to a string so I can use another function (which I already made) to extract the file to a different location that is predetermined.
My button code that opens the file dialog is this:
`a = tkinter.Button(gui, command=lambda: tkinter.filedialog.askopenfilename(initialdir='C:/Users/%s' % user))`
This should be what you want:
import tkinter
import tkinter.filedialog
import getpass
# Need this for the `os.path.split` function
import os
gui = tkinter.Tk()
user = getpass.getuser()
def click():
# Get the file
file = tkinter.filedialog.askopenfilename(initialdir='C:/Users/%s' % user)
# Split the filepath to get the directory
directory = os.path.split(file)[0]
print(directory)
button = tkinter.Button(gui, command=click)
button.grid()
gui.mainloop()
If you know where the file actually is, you could always just ask for a directory instead of the file using:
from tkFileDialog import askdirectory
directory= askdirectory()
Then in the code:
import tkinter
import tkinter.filedialog
import getpass
from tkFileDialog import askdirectory
# Need this for the `os.path.split` function
import os
gui = tkinter.Tk()
user = getpass.getuser()
def click():
directory= askdirectory()
print (directory)
button = tkinter.Button(gui, command=click)
button.grid()
gui.mainloop()