Tkinter program consumes all RAM/CPU - python

I ran this code and the RAM in my computer with my processor looks like it's going to explode! What is the reason?
from tkinter import *
from tkinter import messagebox
from tkinter import ttk
import os
bloque1=Tk()
bloque1.title('Bloque1')
bloque1.config(bg="#1C1C1C")
bloque1.geometry("450x410")
barramenu=Menu(bloque1)
menubar=Menu(bloque1)
menubar.add_cascade(label="Actividades", menu=menubar)
menubar.add_command(label="Instrucciones")
menubar.add_command(label="Ayuda")
menubar.add_command(label="Cerrar", command=bloque1.quit)
bloque1.config(menu=menubar)
bloque1.mainloop()

You are adding a menu to itself. No doubt this is causing an infinite loop inside of Tkinter.
menubar.add_cascade(label="Actividades", menu=menubar)
That menu= attribute needs to be given another menu that will appear when you select that cascade entry from the menubar.

Related

Tkinter freezing Python and Windows Explorer

I often used Tkinter to prompt users and get the path to a file. However, I am facing a recurrent issue, when the filedialog appears it often crashes Windows.
The screen freezes, everything is blocked and when I enter the Task Manager I can see "Python is not responding", when I try to kill Python, then the Task Manager itself freezes and my only option then is to reboot my laptop.
Here is a sample code of what I usually do :
import tkinter as tk
from tkinter import filedialog
import os
window=tk.Tk()
currdir=os.getcwd()
path=filedialog.askopenfilename(parent=window, initialdir=currdir, title="Select file")
Am I doing something wrong ? Any tips ? Is it just bad performances from this library ?
Have you tried using a try/ except block so that you'll exit the loop even if you hit an exception?
import tkinter as tk
from tkinter import filedialog
import os
try:
window=tk.Tk()
currdir=os.getcwd()
path=filedialog.askopenfilename(parent=window, initialdir=currdir, title="Select file")
finally:
window.mainloop()
Include
window.mainloop()
At the end of your GUI file and see if it works that way
Thanks #User9701 and #Linden
Following your advice, I updated my code as per the below :
import tkinter as tk
from tkinter import filedialog
import os
try:
window=tk.Tk()
currdir=sos.getcwd()
path=filedalog.asopenfilename(parent=window,initialdir=currdir, title="Select file")
finally:
window.destroy()

root = tkinter.Tk() or root = Tk()?

I have two scripts that both work:
import tkinter
root = tkinter.Tk()
root.configure(bg='blue')
root.mainloop()
and
from tkinter import *
root = Tk()
text = Text(root)
text.insert(INSERT, "Hello world!")
text.pack()
root.mainloop()
I want to combine the two scripts to print the text on a blue background, but moving anything from one script to another seems to break it.
I can't figure out if it's about root = tkinter.Tk() vs root = Tk(), or import tkinter vs from tkinter import *, or something entirely different. I can't find a successful combination.
I'm using Ubuntu and Python 3.6.9.
Because you use two different styles when importing tkinter, you will need to modify the code from one file when moving to the other. The code in your first example is the preferred way to do it because PEP8 discourages wildcard imports.
When when you copy the code from the second example, you'll need to add tkinter. to every tkinter command (tkinter.Tk(), tkinter.Text(root), tk.INSERT, etc.
Personally I find import tkinter as tk to be a slight improvement. I find tk.Tk() to be a little easier to type and read than tkinter.Tk().
You should know that:
from tkinter import *
will import all the attribute in the tkinter.But if you also define some variable in your script.It will be covered by your new variable.So we don't recommend you to use that.(If you used both from tkinter.ttk import * and from tkinter import *.Some default widgets of tkinter will be covered by ttk widgets.)
Just like Mr.Bryan said,I'd like to use import tkinter as tk,too.

Python tkinter 8.5 import messagebox

The following code runs fine within IDLE, but otherwise I get "NameError: global name 'messagebox' is not defined". However, if I explicitly state from tkinter import messagebox, it runs fine from where ever.
from tkinter import *
from tkinter import ttk
root = Tk()
mainFrame = ttk.Frame(root)
messagebox.showinfo("My title", "My message", icon="warning", parent=mainFrame)
Why does IDLE not need the explicit import statement but elsewhere it is required?
the messagebox is a separate submodule of tkinter, so simply doing a complete import from tkinter:
from tkinter import *
doesn't import messagebox
it has to be explicitly imported like so:
from tkinter import messagebox
in the same way that ttk has to be imported explicitly
the reason it works in idle is because idle imports messagebox for its own purposes, and because of the way idle works, its imports are accessible while working in idle
IDLE is written in Python and uses Tkinter for the GUI, so it looks like your program is using the import statements that IDLE itself is using. However, you should explicitly include the import statement for the messagebox if you want to execute your program outside the IDLE process.
messagebox.showinfo is defined inside tkinter/showinfo.py but when you use from tkinter import * you only import tkinter/__init__.py which holds the definitions of Label, Entry, Button, ... That is how python imports work.
When you use from tkinter import messagebox it looks for messagebox inside tkinter/__init__.py but it can't find it so it tries to import tkinter/messagebox.py
As for the IDLE anomaly, it is a bug in IDLE and I believe that it was patched.

Python - What is tkinter's default window reference?

import tkinter.messagebox
a = tkinter.messagebox.askquestion('','hi')
After the 'askquestion' window closes the tkinter window still remains.
I can resolve this by the following:
import tkinter.messagebox
top = tkinter.Tk()
a = tkinter.messagebox.askquestion('','hi')
top.destroy()
This destroys the window.
My question is:
Is there a way to destroy the window without creating a reference to it?
I tried:
import tkinter.messagebox
a = tkinter.messagebox.askquestion('','hi')
tkinter.Tk().destroy()
but that has no effect.
If you destroy the root window, Tkinter try to recreate one when you call askquestion.
Don't destory the root window. Instead use withdraw.
import tkinter.messagebox
tkinter.Tk().withdraw()
a=tkinter.messagebox.askquestion('','hi')

How to Implement default Windows buttons to a Tkinter program?

why doesn't my python program have default windows 7/8/xp buttons but rather dull windows 2000 buttons? How can I fix this?
My dull buttoned program
What I expect :
Use the ttk module for those effects:
import Tkinter
import ttk
root = Tkinter.Tk()
ttk.Button(text="Hello").grid()
root.mainloop()
Example:

Categories