I'm writing a C++ .dll project, which is injected into another process, and I'm trying to implement embedded Python code. As far as I have described it, this is working. For instance when I inject the .dll, I run the following code:
Py_Initialize();
FILE* PScriptFile = std::fopen("D:\\test.py", "r");
if (PScriptFile) PyRun_SimpleFile(PScriptFile, "D:\\test.py");
test.py is a simple script which opens and writes to a text file. Thus, I know that my .dll is running the script because I can see that this text file is created in the directory of the main executable. Now, I want to run the following script:
import tkinter as tk
window = tk.Tk()
window.protocol("WM_DELETE_WINDOW",window.destroy)
window.mainloop()
I know that this script works on its own. However, when I try to embed it, no Tkinter window pops up.
Thanks
Related
I have a tkinter python code (main GUI) which calls another tkinter python code (child gui that provides input to main gui). I would like to know the trick to use pyinstaller in making a single executable that could compile both these guis.
Note: I have been successful in making executable with a tkinter python code (gui) that have a dependency (just .py file which is not a GUI).
Appreciate your help.
I have made a GUI for my application. All scripts are in Python (2.7) and the GUI is created with Tkinter. I work on Linux, but I needed this app to be executable on Windows. So I've used py2exe, to create an executable. After a while it is working almost perfectly, but I have the following problem:
Somewhere in the application, I need to call external programs, namely ImageMagick and LaTeX. I use the commands convert, pdflatex, simply by importing os module and running os.system(build), where build = 'convert page.pdf page.gif'etc. When those commands are called from the *.exe application the console pops up (meaning a console window opens up for a split of a second and closes again). Is there a way, to prevent this behaviour?
It does not interrupt the application, but it is ugly and not a desired behaviour.
[Note] I chose not to add any samples, since there are lots of files and other content, which, I think, is not related to the problem. I could however try to post minimal (not)working example. But maybe it is not needed.
Thanks!
import subprocess
subprocess.Popen("application.exe", shell = True)
I am trying start learning Tkinter to make a small gui application, however every time I do import tkinter all the print statements are duplicated. The problem exists with the absolute simplest script.
I am running windows 8.1 and python 3.5, and I am writing and building the application in Sublime Text 3 with a customized build system.
It is however unlikely that ST3 has anything to do with it since the problem also exists when running the script from the command line py -3 tkinter.py
Here is an example of my problem. This is all of the code.
import tkinter
print("Hello")
Always outputs
Hello
Hello
But code consisting of just print("Hello") outputs, as expected
Hello
I am truly lost with this problem and it would be of great help if any of you could point me to the right direction
You see two prints as your named your script tkinter.py so the print shows when you run it with py -3 tkinter.py and you see another as you import again in the script. You are not importing from the tkinter lib but from your own script whose name shadows the tkinter lib.
To fix, rename your script and delete any .pyc files in the directory
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
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