Can a tkinter button's command execute root.mainloop() from another gui? - python

I have a tkinter python script, containing a second imported tkinter python script,
What I want to do is to run the root.mainloop() of the second tkinter python script which has been all imported, by clicking a button made in the first tkinter python script.
It is going to be implemented like this :
Button(root, command = Second GUI's root.mainloop())
Is this possible to be achieved ? if not, are there any alternatives available to open a 2nd GUI Window from a button created in the 1st GUI python script ?

Yes, you need to import it to your main script and then call the function from there.

Related

tkinter in Spyder

I am trying my first steps in tkinter. I use Spyder as IDE in Python 3.5.1 |Anaconda 4.0.0.
I want to run the very simple script below but it always crashes my Spyder. In a normal shell/bash it runs though and opens the canvas.
import tkinter as tkr
tk = tkr.Tk()
canvas = tkr.Canvas(tk, width=500, height=500)
canvas.grid()
tk.mainloop()
Under Preferences for the Ipython Console I already tried different settings (i.e. Qt, Automatik, Tkinter) but none of it did help.
What am I doing wrong (and how can I do it better)?
many thanks in advance
update to Spyder 3.0.1
https://pythonhosted.org/spyder/
https://github.com/spyder-ide/spyder/releases/tag/v3.0.1
I just did this on win 10: no crash, got blank "tk" separate window
You should try to change the graphics backend
Go to tools/preferences/I-python Console/Graphics and in backend change it to Tkinter.
That should do it!!
In the Spider menu bar, go to: Tools > Preferences
A window will open, then on left side go to: Completion and linting
In the right side, go to: introspection and below you will see different modules available in your current spider
In the section "Preload the following modules...", add tkinter to the end of the list
I found this suggestion while trying to get tkinter to display a GIF image in a label. First, on a Mac, the Tools menu item does not contain Preferences--look under the "python" menu instead. Even so, I have the
IPython Console Graphics backend set to Tkinter and tkinter is added to Completion and Linting. But all I get when launching from Spyder is an empty frame entitled "tk #442", whereas when I launch from the command line I get a frame with a nice GIF image as expected.

How do I run a .py(GUI) file outside of IDLE in order to use sys.exit

How do I run a .py file outside of IDLE in order to use sys.exit? I'm using command=sys.exit and seems to crash inside of tcl & tk when the button is pushed. The exit is working cause it returns SystemExit in the Shell. I've found that I need to run this outside the IDLE to have this work properly. Oh, I'm working from the book "Programming Python" tutorial. How do I run a .py(GUI) file outside of IDLE to see sys.exit work?
import sys
from tkinter import *
widget = Button(None, text='Hello widget world', command=sys.exit)
widget.pack()
widget.mainloop()
1.- In windows double click on the file.
2.- For linux, if not already there, add a shebang first, telling where python is.
Shebangs are like:
#!/usr/bin/python
or
#!/usr/bin/env python
also remember the dot-slash if you call the program from console:
./myscript.py
3.- In OSX follow same as in linux. Also see these specific instructions and these other ones

Script works in IDLE, but .py-file doesn't work

I have a tkinter script, which runs just fine in IDLE. However, when I double click the .py-file from Windows Explorer, the console window flashes half a second and then it exits.
I was able to screenprint the console window. It says:
...etc.etc...
NameError: global name 'simpledialog' is not defined
simpledialog is a module in tkinter which I use in my script. As I do from tkinter import *, there is no need to explicitly write tkinter.simpledialog.
It works in IDLE, why not as .py?
IDLE uses Tkinter as its graphical environment. It is possible that your code is relying on a side effect of an import by IDLE itself. This is especially true if you use IDLE without a subprocess.
The simpledialog module does not import when using from tkinter import *.
Try adding this to your code:
import tkinter.simpledialog as simpledialog
Have you updated your PATH environment variable so that your Python executable is found? You can find more information on how to do here - Using Python on Windows
But you basically need to make sure that the folder containing python.exe (e.g. C:\Python32) is displayed when you type the following command from a prompt:
echo %PATH%
I had exactly the same problem with one of my scripts utilizing Tkinter.
Adding call to mainloop() fixed the issue.
See this tutorial for an example: [http://sebsauvage.net/python/gui/#import1
In my case, in the init function I have
def __init__(self,Width=400, Height=400):
# Create GUI window ------------------------------
win = Tk()
...
in the end of init I added:
win.mainloop()
Now it works by just running the file.
Hope this helps
Similar trouble for me just now, in my first week with python. But I dimly remembered a similar problem with a simple early test script and thought the trouble then was # comments.
So I tried that with my Tkinter infused .py script. It ran fine in IDLE as you say, then only flashed when clicked in windows. But there were a couple # commented lines at the top of file.
I took them all out and it now runs no sweat directly in windows. Have a look .. for #.
Sorry, can't seem to delete this post. Now the files work #comments included. Don't know what's up with that. ..
I found that changing the executable py file to a file.pyw fixed the problem. This tells python to execute it using the pythonw.exe which runs the script without the terminal/console in the background.
Not sure why this works, perhaps some screwed up environment variables from a previous python installation.
Changing the file's extension to pyw instead of py might solve the problem

build cmd into Tkinter window

Hi i was wondering if you could have the comand prompt box pop into the Tkinter window when you start the program? Some thing like:
from Tkinter import *
admin = Tk()
cmd = Cmd(admin)
cmd.pack()
admin.mainloop()
I'm on windows
http://tkinter.unpythonic.net/wiki/CmdTkHere is what you want, this is not just opening a cmd window. its embedding cmd.exe into a Tkinter.Frame. And a note here if you rename the python script to ".pyw" extension, the console will be hidden. Except for in a virtual environment's.
I don't believe there is any built in console widget. It may be possible to whip up a custom one using the Tkinter Text widget. However, that would take a bit of effort/time.
Another possible option is simply have your program launch command prompt.
Two different ways to launch command prompt on a Windows machine.
import subprocess, os
subprocess.Popen('cmd.exe')
os.system("cmd.exe")
EDIT:
Unforunetly I don't believe there is any built in widget like that. However I thought of another possible solution, check out the code for the IDLE GUI, it has a console and the GUI portion is entirley written using Tkinter. So you may be able to utelize that code.

How to launch another program in GUI (python tkinter)

i wrote a simple GUI using Python Tkinter. When i click a button it launches another program. But the GUI window stays there waiting for the the program to exit. I want the GUI to launch the program on Button1 and I can click on Button2 to run the test. I used os.system() to launch the program. Pls advise.
thanks
maximus
Yes, os.system() will wait.
As the documentation for os.system() points out, you might want to replace it by using the subprocess module, where there are many different ways to start commands and where you can choose if you want to wait for it to finish or not.

Categories