Why does my Tkinter code take too long to run? - python

I have a code in Python and I am trying to create a UI for it using Tkinter. I just started off using Tkinter, and got the following code :
from Tkinter import *
window = Tk()
window.title("Hello world")
window.mainloop()
I am using Jupyter notebook to run the code and it's been several minutes and the code still hasn't given me an output. What exactly wrong with this? Is it the code or Jupyter Notebook?

Probably is because in the first line "Tkinter" has to be all in lower case.

Related

While importing pywinauto in Tkinter window size changes

Here's a snippet of my test code
from tkinter import *
master=Tk()
master.geometry('640x340')
Button(master,text='check').pack()
def new():
print('done')
from pywinauto import Desktop, Application
master.after(1000,new)
master.mainloop()
In exactly one second the tkinter window which is created reduces in size, same thing happens for the button as well. This is just test code which I wrote after tracking down this problem in the application I am trying to build. Could somebody tell me why the window changes in size, and what I can do to prevent this?
I have checked all I could, the official documentation only mentions Tkinter once and that is referring to pywinauto’s backend, and I don't think it is relevant. Here on page 5.

I'm struggling with being able to close a simple widget using tkinter

I have some Python experience and wish to learn about GUI development using tkinter.
I can create a simple widget, but when I try to close the window, things "hang" and nothing happens. Only by restarting the Python kernel can I get the window to close.
I'm running Python 3.7 using Spyder and, based upon some simple examples I've found in other forums tried the following:
import tkinter
root = tkinter.Tk()
root.title("Hello!")
simple_label = tkinter.Label(root, text="Easy, right?")
closing_button = tkinter.Button(root, text="Close window",
command=root.destroy)
simple_label.pack()
closing_button.pack()
root.mainloop()
As I mention above, the window does not close when I click my mouse on the Close Window button. I just get the "swirly" indicator on my Mac indicating the program is not responding. However, I am able to perform calculations in the Spyder console.
The code works fine, but what you should do is open the program in the Python IDLE that comes when you download Python. When you run the program it should open up the Python Launcher, which allows you to interact your your GUI (And it will let you close click the close button) I am using a Mac and everything works fine.
I was analyzing your case and we BOTH did something we didn't notice after.
When you use the "root.destroy" command, you should add parenthesis after.. Had the same thing happening to me
Added root.destroy ( ) and worked flawlessly. <-- Use it without SPACES
Hope it helps someone in the future!

Tkinter Gui does not appear

I'm trying to learning Python following a video Tutorial found on You Tube.
The version of Python is 2.6.4 and the OS is Windows 10 64.bit.
Yesterday the tutorial face the creation of GUI.
The miminum instracion set is in a file:
from Tkinter import *
top = Tk()
Running this file in the Python shell of Windows a little window appares.
Yestreday all was running correctly.
The tutorial adds labels and button.
Today I restarted but running the same example the window does not apper.
Could you help me to understand what is wrong?
Ther are some process open?
I have to to some kind of restet.
Thank you
You forgot to start the Tkinter GUI. You should add .mainloop() for that. So:
from Tkinter import *
top = Tk()
top.mainloop()

How to get a TkInter file selection dialogbox to work with IPython / Spyder?

I am trying to create a simple file selection dialog for one of my scripts and I was trying to use the code examples from this thread: Quick and easy file dialog in Python?
import Tkinter, tkFileDialog
root = Tkinter.Tk()
root.withdraw()
file_path = tkFileDialog.askopenfilename()
I also tried using the easygui module that uses Tkinter to do the same thing. In both cases, the code above hangs the IPython console. I gather that this has something to do with event loops, but I have no real experience with GUIs in Python.
Could someone point me in the right direction on how to get a dialogbox for file selection to work with IPython/Spyder. For the record, I am on Python 2.7.6 and IPython 2.4.1
Before running the code above, you need to set the right event loop (in this case Tk), as you correctly guessed.
To do that you need to run this command:
In [1]: %gui tk
and then run your code.
Note: To access the documentation about the %gui magic in Spyder, you need to place the cursor in front of %gui and press Ctrl+I, like this
In [1]: %gui<Ctrl+I>

Maximize button does not work using Tkinter in RHEL5

I created a minimal Tkinter GUI as follows:
import Tkinter
root = Tkinter.Tk()
root.mainloop()
Everything is fine if I run the above code on RHEL5, except that the maximize button does not work properly(resizing is available). If I click the button, the window does not expand to occupy the whole screen. And I belive this issue is platform-specific, because there is no such issue for the same code on Windows.
Does anyone know the reason for this? Is there any solution? Thanks!
It works for me in Fluxbox. Try putting something in root like a label so it has something to display. Probably won't make any difference but worth a try.

Categories

Resources