Author Topic: How to use tkinter from stealth [hackish work-around]  (Read 3472 times)

0 Members and 1 Guest are viewing this topic.

Offline BoydonTopic starter

  • Moderator
  • **
  • *****
  • Posts: 76
  • Activity:
    0%
  • Reputation Power: 3
  • Boydon has no influence.
  • Respect: +16
  • Referrals: 0
    • View Profile
How to use tkinter from stealth [hackish work-around]
« on: April 06, 2012, 06:23:36 AM »
0
If you've ever tried to use tkinter from stealth you've probably found that it gives you a strange and bad error.

I'll try to make a sample to explain.
This is very basic sample that will run in a clean python 3.2 environment:
Code: [Select]
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()

If you run it int stealth you'll get this error:
Quote
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'

So the only way you have to go is to force sys.argv to an empty list:
Code: [Select]
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()

I think this is a bug in stealth, but in the way showed before you can bypass it. :)

Thanks to Crome969 that pointed out the problem and gave me an idea on how to solve it. :)
Member of the Stealth development team.

Tags: