Non useful tkinter window appears in spyder - python

Question 1: I have a non useful window that appears when using tkinter in spyder.
Any solution for this issue ?
Question 2: Why there is a warning message on 'from tkinter import *' ?
Code:
from tkinter import *
from tkinter.simpledialog import askstring
from tkinter import messagebox
box = Tk()
name = askstring('Name','What is your name?')
messagebox.showinfo('Hello!','Hi, {}'.format(name))
box.mainloop()

The "non useful" window is simply box.
messagebox will open a new window. So you can just remove box if you don't intend to use it further.
It's usually not recommended to import everything from a module because it could cause name conflicts with other modules or built-in function:
import tkinter as tk
from tkinter.simpledialog import askstring
name = askstring('Name','What is your name?')
tk.messagebox.showinfo('Hello!','Hi, {}'.format(name))

The additional window is the instance of Tk most often named root cause every other window or widget is a child of the root window. You will need it to initiate your messagebox but if you don't want to look at it you have several choices.
My personal recommendation is to us overrideredirect which will discard it from the taskbar and use withdraw to actually hide it in the screen/monitor. But you may prefer wm_attributes('-alpha', 0) over it to make it opaque/transparent.
Avoiding wildcard imports is recommanded because of name clashes/collisions. For example tkinter has a PhotoImage class, so does pillow. If you have wildcard imports on both, on PhotoImage will overwrite the other in the global namespace.
Code:
import tkinter as tk
from tkinter.simpledialog import askstring
from tkinter import messagebox
box = tk.Tk()
box.overrideredirect(True)
box.withdraw()
name = askstring('Name','What is your name?') #blocks the code block
messagebox.showinfo('Hello!','Hi, {}'.format(name)) #shows message
box.after(5000, box.destroy) #destroy root window after 5 seconds
box.mainloop()#blocks until root is destroyed

For the first question answer is that you don't need to create box because function askstring create frame on it's own. So if the whole program is just to ask for the name and to greet user, you are perfectly fine with just this piece of code:
from tkinter import *
from tkinter.simpledialog import askstring
from tkinter import messagebox
name = askstring('Name','What is your name?')
messagebox.showinfo('Hello!','Hi, {}'.format(name))
And for the second question you need to post what warning you get.

Related

tk window doesn' t pop up

Hi I'm new to python and Tkinter. When I execute the code, nothing is happening an there isn't error. After less than one second,the code finish to process.
from tkinter import *
w1 = Tk()
w1.mainloop'
First of all, you used
from tkinter import *
so tkinter.Tk isn't a thing, since tkinter isn't in the namespace, instead you must just do Tk
Also, tkinter.Tk is a class, and for it to work, needs to be instantiated. w1.mainLoop is a function, and needs to be called.
Therefore, a working code should be
from tkinter import *
w1 = Tk()
w1.mainloop()
I believe you already fixed your typo w1.mainloop' to w1.mainloop().
I want to recommend using import tkinter as tk, so you don't overwrite you namespace.
Then your code could look like this:
import tkinter as tk
w1 = tk.Tk()
w1.mainloop()

Python tkinter simpledialog

I have a Python script which generates a GUI window with tkinter library. I'd like to make some of it's buttons display a prompt - small window to ask the user for some number (something like in JavaScript). I tried the following command:
x = tkinter.simpledialog.askstring
But it returns an error:
NameError: name 'tkinter' is not defined
and no prompt is generated, although I have imported the library in the script's beginning:
from tkinter import *
from tkinter import simpledialog
Other elements (buttons, labels etc.) in the main window work correctly. Please help.
askstring is part of tkinter.simpledialog so you might import it like so
from tkinter.simpledialog import askstring
usage example
import tkinter as tk
from tkinter.simpledialog import askstring
root = tk.Tk()
x = askstring("Title", "Prompt")
print(x)
root.mainloop()

Removing Base Window When Using Tkinter Python 3 (Askopenfiledialogue)

This is my first post on here, so bear with me as far as post etiquette goes. I've been struggling to get rid of the base Tkinter window that appears when I'm using the askopenfilename function of Tkinter. I've tried using Withdraw and destroy (in combination and alone) to fix this issue, but it seems to leave my code stuck in a loop and unable to continue to the next sections.
I have seen several solutions to this in Python 2, but I have no idea how they translate to python 3. Tkinter isn't a module I have a lot of experience with, so it is likely a simple oversight I am making.
Any suggestions or comments are much appreciated
Here is a sample of my code I am using (CSV module is for another section of my code)
import csv
from tkinter import filedialog
from tkinter import *
root= Tk()
root.filename= filedialog.askopenfilename(initialdir = r"\Users",title="Select A File", filetypes= [("Csv Files","*.csv")])
root.mainloop()
Use withdraw to hide root window, deiconify to show root window
import csv
from tkinter import filedialog
from tkinter import *
root=Tk()
# Hide Window
root.withdraw()
filename= filedialog.askopenfilename(initialdir = r"\Users",title="Select A File", filetypes= [("Csv Files","*.csv")])
# Show Window
root.deiconify()
root.mainloop()

Issue with Scale widget using tickinterval

The widget shows tick using tickinterval when following code shown below,
from Tkinter import *
slider_1 = Scale(mGui,orient=HORIZONTAL,length = 100,from_=0,to=9, tickinterval =1).pack()
However it throws error with the following code
from Tkinter import *
from ttk import *
slider_1 = Scale(mGui,orient=HORIZONTAL,length = 100,from_=0,to=9, tickinterval =1).pack()
Error:
_tkinter.TclError: unknown option "-tickinterval"
Why is it so? Is it a bug or problem with the installation. For information i am using Python 2.7.10
This is because the ttk module contains also a Scale widget, and you are actually using the Scale widget from ttk and not from Tkinter. Widgets in the ttk module are customised and styled differently from Tkinter widgets.
Check the following documentation on ttk for more information regarding its widgets:
ttk — Tk themed widgets
To solve your problem, you could remove your second global import and simply do:
import ttk
Then, every time you want to use a widget from ttk, you can simply prefix it with ttk..

Tkinter importing without *?

In my past programming i used the following code:
from tkinter import *
Gui = Tk()
but someone told me that importing * was not good for many reasons but now when i want to import
from tkinter import geometry
it says geometry not a module thing (name).
and when i do:
import tkinter
tkinter.geometry(500x500)
it says 'module' object has no attribute 'geometry'
Can someone explain me how to import with tkinter all the different ways?? Not only for geometry but most of the tkinter modules...???
That's because the module tkinter doesn't have a geometry function. It's the Tk instances that do.
Here's a good way to accomplish what you're trying to do:
import tkinter as tk # tk is a convenient, easy to type alias to use for tkinter.
gui = tk.Tk()
gui.geometry("500x500") # don't forget the quotes
Why from tkinter import * is a non-ideal way to import tkinter
As an aside, whoever told you that from tkinter import * was a bad idea was correct - when you do that, you load all of tkinter's namespace into your module's namespace.
If you do that, you can end up with unpleasant namespace collisions, like this:
from tkinter import *
gui = Tk()
Label = "hello"
Label1 = Label(gui, text=Label)
# Traceback (most recent call last):
# File "stackoverflow.py", line 98, in <module>
# Label1 = Label(gui, text=Label)
# TypeError: 'str' object is not callable
You've overwritten the reference to tkinter's Label widget - so you can't create any more Labels! Of course you shouldn't be capitalizing local variables like that anyways, but why worry about avoiding those namespace problems when you can do this instead:
import tkinter as tk
This ^^^^ import method is also preferable because if at some point you want to swap Tkinter out for another module with a similar implementation, instead of combing through your code for all elements of the Tkinter module, you can just go like this:
#import tkinter as tk
import newTkinter as tk
And you're all set. Or, if you want to use another module that happens to have some of the same names for its classes and methods, the following would cause chaos:
from tkinter import *
from evilOverlappingModule import *
but this would be fine:
import tkinter as tk
import evilOverlappingModule as evil
The reason that from module import * is considered harmful is that it pollutes the main namespace with every public name in the module. At best this makes code less explicit, at worst, it can cause name collisions. For example, module codecs has an open method defined, and there is the built-in version, which take different arguments. If I write
from codecs import *
f = open(…)
which open will I get? Tkinter has a lot of symbols, and tkinter based programs tend to use them very heavily. better than the import * is
import tkinter as tk
which then allows the — still explicit, but easier to type and read:
tk.Tk().geometry(…)
If you * imported tkinter, essentially tkinter. is in the namespace, meaning that to access to tkinter modules without worrying about prefixing it with tkinter.. In this case, geometry("500x500") is a method of Tk(), so you would use it like this
from Tkinter import *
Gui = Tk()
Gui.geometry("500x500")
Gui.mainloop()
Similar objects, such as various widgets, etc. are used the same. For example, a label would be made like this:
from Tkinter import *
Gui = Tk()
label= Label(Gui, text="Hello World!")
label.pack()
Gui.mainloop()
I don't know why someone said that importing * wasn't good cause that isn't true, it's actually better then just importing tkinter. Importing * will make the programming easier. With just tkinter you would need to type tkinter. before doing something, or if you do it as tk, you would need to do tk., from tkinter import * is the best what you can do.
Instead of doing:
from tkinter import *
You can do:
from tkinter import Tk, Canvas #The widgets you want to use
Or:
import tkinter as tk
Here’s a quick answer: Tkinter doesn’t have a geometry function. You can use the geometry function with instances of the Tk() class.
Example:
import tkinter as tk # Use the module name ’Tkinter’ if you have Python 2
root = tk.Tk()
root.geometry(‘500x500’) # You can change the size
# Other code goes here
root.mainloop()
Just like the geometry function, the mainloop, pack, grid, place, config etc. functions are used with the instances of the class Tk()
Thank You! Hope that works!

Categories

Resources