0

When I am trying to run my program based off a sample Tkinter GUI, nothing happens. I am still new to Pydev, but I find this rather unusual. I have a Main.py file containing the code, and I try to simply run the module without any success.

I simply copy/pasted from this reference,

# Main.py
import tkinter as tk;

class Application(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.pack()
        self.create_widgets()

    def create_widgets(self):
        self.hi_there = tk.Button(self)
        self.hi_there["text"] = "Hello World\n(click me)"
        self.hi_there["command"] = self.say_hi
        self.hi_there.pack(side="top")

        self.quit = tk.Button(self, text="QUIT", fg="red",
                              command=root.destroy)
        self.quit.pack(side="bottom")

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

root = tk.Tk()
app = Application(master=root)
app.mainloop()

The only result of running the module is an empty Liclipse console dedicated to Main.py. I have also tried other examples from other sites with no luck. Also, I am currently on MacOS if it matters.

martineau
  • 119,623
  • 25
  • 170
  • 301
Boooke
  • 5
  • 1
  • 3
  • Did you maybe forget to include `import tkinter as tk` at the beginning of your code? – Josselin Apr 20 '17 at 11:09
  • No, but maybe I should've added that to the code I included here. I'll add the correct code to the question, thanks for noticing. I assume the code runs fine since the console outputs no errors - or anything at all for that matter. – Boooke Apr 20 '17 at 11:54

1 Answers1

0

Are you sure you have everything properly (working directory, python path...) configured in Liclipse? I have just tried in a fresh install and, after configuring Liclipse to run the current project in Python 3.6 and after selecting the main project file as being this source code, it runs and shows a window with a button, as intended, and it also prints the text to console.

Also, it does not feel very "pythonic" to initialize a button that way. I would rather suggest like this:

# Main.py
import tkinter as tk;

class Application(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.pack()
        self.create_widgets()

    def create_widgets(self):
        mytext = "Hello World\n(click me)"
        self.hi_there = tk.Button(self, text=mytext, command=self.say_hi)
        self.hi_there.pack(side="top")

        self.quit = tk.Button(self, text="QUIT", fg="red", command=root.destroy)
        self.quit.pack(side="bottom")

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

root = tk.Tk()
app = Application(master=root)
app.mainloop()

The code looks a little more readable and works in the same way. When you start growing your interface, it will be many lines long and by reducing two lines at each button, you can make it a lot shorter. I have been coding a tkinter app with more that 1500 lines of code and at that point I promised myself that I would try to learn how to keep it more organized and short ;)

Victor Domingos
  • 1,003
  • 1
  • 18
  • 40
  • Just for a heads up, I saw your comment, thank you, and I am currently trying to figure out a way to make a clean install of Python again. I had classes a long time ago, where they required us to use a package manager that caused me trouble later on, and I then thought to have uninstalled. It caused what appeared to be some "path/reference" problems in Python, and maybe it is still what is causing it. – Boooke Apr 24 '17 at 09:51
  • @MikkelBugge Is PyDev really needed for your setup? I have been doing fine with Atom (and some plugins), CodeRunner and Textwrangler, for instance. I have tried PyCharm, which seems to be one of te most mentioned Python IDEs, but I never managed to run tkinter apps from it, so I skipped that one. – Victor Domingos Apr 24 '17 at 10:13
  • To be honest, I just picked the first that most seemed to agree with on different fora. I don't really need anything specific, and I try to use as few packages as possible, so I'm sure anything goes - Anything more lightweight would be nice, I'll check them out, thanks :-) – Boooke Apr 25 '17 at 10:21
  • It seems you are right. I suspect Pydev doesn't seem to have been correctly set up, since tkinter works in Pycharm. I ended up trying Pycharm simply because it has an easy packet manager included (I had trouble installing pyserial for 3.6 on my own), and cleaning up Python directories on a Mac is something I am too lazy to do at the moment. Thanks for the suggestions, though! – Boooke Apr 26 '17 at 10:53
  • @MikkelBugge mind that I had trouble using PyCharm with tkinter (couldn't run the tkinter app from inside PyCharm). Not sure if it is supported. – Victor Domingos Apr 26 '17 at 10:59