Python TkInter create a button to open a specific directory - python

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.

Related

Why tkinter filedialog is looking different after using a pyglet window?

I have used a tkinter.filedialog with the pyglet library. But an old file explorer pops up when I open that filedialog. Normally it opens the native/modern file explorer of windows, but it is showing this behavior after importing the pyglet module. Any solution to get back the new filedialog?
Here is a basic code to reproduce the issue:
from tkinter import Tk, Button, filedialog
import pyglet #Try commenting this import to test the filedialog
root= Tk()
root.geometry("200x100")
window = pyglet.window.Window(20, 20, "Pyglet_window") #Pyglet window
def open_file():
file=filedialog.askopenfilename()
btn= Button(root, width=10,bg="#FFFFFF",fg="black", text="Open File",command=open_file)
btn.grid(padx=50,pady=20)
root.mainloop()
My results:

How to properly import tcl file

I am trying to use the Azure theme in a tkinter application i made. I used the code from github to import the theme(https://github.com/rdbende/Azure-ttk-theme). I put the "azure.tcl" file in the folder with the python file and got the error "_tkinter.TclError: couldn't read file "./theme/light.tcl": no such file or directory". I then tried making a simpler tkinter program with the same ttk theme import code from before but this time in it's own folder with the tcl file. I still got the same error message. When i download the file from github, i ran the example python file and worked just fine.
Here is the code i use to import the file.
import tkinter as tk
from tkinter import Label, ttk
root = tk.Tk()
root.tk.call("source", "azure.tcl")
root.tk.call("set_theme", "dark")
root.geometry("300x300")
L1 = Label(root, text="Hello world")
L1.grid(row=1, column=1)
root.mainloop()
If you do not have this "TKinterModernThemes" library, then you should also install it with below code:
pip install TKinterModernThemes
Then, you can keep your script as it is but just a few arrangements.
Importing Library:
import TKinterModernThemes as TKMT
Threat like window.root is your tk.Tk() object:
import tkinter as tk
from tkinter import Label, ttk
import TKinterModernThemes as TKMT
window = TKMT.ThemedTKinterFrame("%yourProjectName","azure","dark")
window.root.geometry("300x300")
L1 = Label(window.root, text="Hello world")
L1.grid(row=1, column=1)
window.root.mainloop()

tkinter askdirectory doesn't work from imported module

I have been using tkinter to supply a file dialog (in python 3.6) which allows users to select a directory. It works fine when it is a sub-function within the same module, but if I move that subfunction into a separate module and then try to import it from that module, it no longer works. Instead the code just hangs when the file dialog should pop up but it never appears.
working code:
This works if I run the main function
from tkinter import Tk
from tkinter.filedialog import askdirectory
def SelectDirectory():
# start up the tk stuff to have a file directory popup
print('start')
root = Tk()
print('postroot')
root.withdraw()
print('postwithdraw')
# let the user pick a folder
basepath = askdirectory(title='Please select a folder')
print('postselection')
root.destroy()
print('postdestroy')
return basepath
def main():
ans = SelectDirectory()
print(ans)
If I instead put this in another module and import it from that module, then it will print until 'postwithdraw' and then just hang.
submod.py:
from tkinter import Tk
from tkinter.filedialog import askdirectory
def SelectDirectory():
# start up the tk stuff to have a file directory popup
print('start')
root = Tk()
print('postroot')
root.withdraw()
print('postwithdraw')
# let the user pick a folder
basepath = askdirectory(title='Please select a folder')
print('postselection')
root.destroy()
print('postdestroy')
return basepath
and then run this:
from submod import SelectDirectory
def main():
ans = SelectDirectory()
print(ans)
It never gets past 'postwithdraw' and the file dialog never pops up.
Does anyone know what I am doing wrong here? I would think it has something to do with the tkinter window not appearing since its not the main module but is there some way to get past that?
Your both versions don't work. Both give 'module' object is not callable.
You have to use
root = Tk.Tk()
instead of
root = Tk()
and then both versions works.
Maybe two Tk in Tk.Tk() seems weird but usually we use lowercase tk instead of Tk in
import tkinter as tk
and then you have
root = tk.Tk()

How do I create an Import File Button with Tkinter?

So you know how when you use Notepad (on Windows), for example, and you want to open an old file? You click file, then open, then a file dialog opens ups and you can select the file you want, and the program will display its contents.
Basically, I want to make a button in Python that can do that exact thing.
Here's my function for the button-
def UploadAction():
#What to do when the Upload button is pressed
from tkinter import filedialog
When I click on the button assigned to this action, nothing happens, no errors, no crash, just nothing.
import tkinter as tk
from tkinter import filedialog
def UploadAction(event=None):
filename = filedialog.askopenfilename()
print('Selected:', filename)
root = tk.Tk()
button = tk.Button(root, text='Open', command=UploadAction)
button.pack()
root.mainloop()

Tkinter not working in cmd (working in IDLE)

I am trying to display a directory selection dialog box (for getting a path and then for saving downloaded stuff).The code runs fine in IDLE but when i try to run it in CMD i get this error
NameError: name 'Tk' is not defined
I am using tkinter for gui.
Code Snippet
from tkinter import filedialog
root = Tk()
root.withdraw()
filename = filedialog.askdirectory()
Using Python 3.4.3. Any help/suggestions?
The statement from tkinter import filedialog only imports the filedialog module from tkinter. If you want the usual Tkinter stuff, you have to import that too. I'd recommend import tkinter as tk and then referring to it with e.g. root = tk.Tk() so you don't just dump everything into the global namespace. Or, if you really just want the root object, use from tkinter import Tk.
from tkinter import Tk
from tkinter import filedialog
root = Tk()
root.withdraw()
filename = filedialog.askdirectory()

Categories

Resources