Tkinter not working in cmd (working in IDLE) - python

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()

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()

Python TkInter create a button to open a specific directory

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.

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()

tk messagebox import confusion

I'm just beginning to learn tkinter at the moment, and when importing messagebox I found that I must not really understand import statements.
The thing that confuses me is that:
import tkinter as tk
def text_box():
if tk.messagebox.askokcancel("Quit", "Never Mind"):
root.destroy()
root = tk.Tk()
button = tk.Button(root, text="Press the button", command=text_box)
button.pack()
root.mainloop()
compiles fine, but pressing the button gives the error 'module' object has no attribute 'messagebox', while the code:
import tkinter as tk
from tkinter import messagebox
...
if messagebox.askokcancel("Quit", "Never Mind"):
...
...works without a hitch.
I get a similar error if I import with from tkinter import *.
The help for tkinter shows messagebox in the list of PACKAGE CONTENTS, but I just can't load it in the normal way.
So my question is, why...and what is it about importing that I don't understand?
Just thought I should mention—the code only works in Python 3, and in Python 2.x messagebox is called tkMessageBox and is not defined in tkinter.
tkinter.messagebox is a module, not a class.
As it isn't imported in tkinter.__init__.py, you explicitly have to import it before you can use it.
import tkinter
tkinter.messagebox # would raise an ImportError
from tkinter import messagebox
tkinter.messagebox # now it's available eiter as `messagebox` or `tkinter.messagebox`
try this
import sys
from tkinter import *
... and your code

Categories

Resources