Forum in READ ONLY mode! All questions and discussions on Discord official server, invite link: https://discord.gg/VxsGzJ7

Stelth and Python tkinter

тут можно задать вопрос по скриптингу
Post Reply
Boydon
Neophyte
Neophyte
Posts: 36
Joined: 12.02.2012 18:06

Stelth and Python tkinter

Post by Boydon »

Hello,

I'm having problems using Python tkinter from stealth. I'm running stealth on Python 3.2 (but I think that it is the same with Python 2.7).

Here a very simple snippet that works perfectly when i run it from clean python 3.2 console (not from stealth):

Code: Select all

from tkinter import *

class App:

    def __init__(self, master):

        frame = Frame(master)
        frame.pack()

        self.hi_there = Button(frame, text="Hello", command=self.say_hi)
        self.hi_there.pack(side=LEFT)

    def say_hi(self):
        print ("hi there, everyone!")


root = Tk()

app = App(root)

root.mainloop()
When run is stealth the error I get is this:
16:04:58:079 []: File "C:\Python32\Lib\tkinter\__init__.py", line 1669, in __init__
baseName = os.path.basename(sys.argv[0])
AttributeError: 'module' object has no attribute 'argv'
The only hackish work around I've found is to force the sys.argv in a way like this:

Code: Select all

from tkinter import *
import sys

class App:

    def __init__(self, master):

        frame = Frame(master)
        frame.pack()

        self.hi_there = Button(frame, text="Hello", command=self.say_hi)
        self.hi_there.pack(side=LEFT)

    def say_hi(self):
        print ("hi there, everyone!")

sys.argv = [""]

root = Tk()

app = App(root)

root.mainloop()
The problem is that this looks really unclean to me and I'm not sure to do the right thing. Can this be checked? Because if you look at python documentation of Py_Initialize() it says:
Initialize the Python interpreter. In an application embedding Python, this should be called before using any other Python/C API functions; with the exception of Py_SetProgramName(), Py_SetPythonHome(), PyEval_InitThreads(), PyEval_ReleaseLock(), and PyEval_AcquireLock(). This initializes the table of loaded modules (sys.modules), and creates the fundamental modules __builtin__, __main__ and sys. It also initializes the module search path (sys.path). It does not set sys.argv; use PySys_SetArgvEx() for that. This is a no-op when called for a second time (without calling Py_Finalize() first). There is no return value; it is a fatal error if the initialization fails.
Thank you and thanks to Crome969 that pointed out the problem and gave me an idea on how to solve it. :)
Post Reply