Python Forum
[Tkinter] scipt with apparently no errors shown, still not running
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] scipt with apparently no errors shown, still not running
#1
under is the actual script, I'm still on the learning side. I have done others before this particular one and they all executed and popped up the GUI. This one hasn't been able to run and only shows "Process finished with exit code 0".
it's on PyCharm Community Edition 2024.1.4, Python 3.12.




import tkinter as tk


class MyGUI:

    def __int__(self):

        self.root = tk.Tk()

        self.label = tk.Label(text='My message', font=('Arial', 18))
        self.label.pack(padx=10, pady=10)

        self.textbox = tk.Text(height=3)
        self.textbox.pack(padx=10, pady=10)

        self.check_state = tk.IntVar()

        self.check = tk.Checkbutton(text='State check', font=('Arial', 16), variable=self.check_state)
        self.check.pack(padx=10, pady=10)

        self.button = tk.Button(text='Show message', font=('Arial', 18), command=self.message)
        self.button.pack(padx=10, pady=10)

        self.root.mainloop()

    def message(self):
        print(self.check_state.get())


MyGUI()
Output:
Process finished with exit code 0
Reply
#2
Look at the __init__
you have __int__
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
That is an odd way to write a tkinter program.

A class should be something you can easily describe. What is MyGui? MyGUI is not a window, is not a widget, is not some kind of data object. The only short description that fits is "It is a tkinter program" and that is not a good thing to turn into a class. This is identical to your code.
import tkinter as tk


def message():
    print(check_state.get())


root = tk.Tk()
label = tk.Label(text="My message", font=("Arial", 18))
label.pack(padx=10, pady=10)
textbox = tk.Text(height=3)
textbox.pack(padx=10, pady=10)
check_state = tk.IntVar()
check = tk.Checkbutton(text="State check", font=("Arial", 16), variable=check_state)
check.pack(padx=10, pady=10)
button = tk.Button(text="Show message", font=("Arial", 18), command=message)
button.pack(padx=10, pady=10)


root.mainloop()
Making it a class just made the code a little longer without providing any benefit. Using classes does not make code object oriented, and using classes does not make code better.

I might write your code using a class, but I would subclass tk.Tk. Something like this.
class Window(tk.Tk):

    def __init__(self):
        super().__init__()
        label = tk.Label(text="My message", font=("Arial", 18))
        self.textbox = tk.Text(height=3)
        self.check_state = tk.IntVar()
        check = tk.Checkbutton(text="State check", font=("Arial", 16), variable=self.check_state)
        button = tk.Button(text="Show message", font=("Arial", 18), command=self.message)

        label.pack(padx=10, pady=10)
        self.textbox.pack(padx=10, pady=10)
        check.pack(padx=10, pady=10)
        button.pack(padx=10, pady=10)

    def message(self):
        self.textbox.insert(tk.END, str(self.check_state.get()))


Window().mainloop()
Not a lot of difference, but at least Window has an identity. It is a customized tk.Tk window. Notice that I don't save label, check or button as instance variables. If something only appears in __init__() it should not be an instance variable.

For a more reuseable window you can make a view.
class MyGui(tk.Frame):

    def __init__(self, parent):
        super().__init__(parent)
        label = tk.Label(text="My message", font=("Arial", 18))
        self.textbox = tk.Text(height=3)
        self.check_state = tk.IntVar()
        check = tk.Checkbutton(text="State check", font=("Arial", 16), variable=self.check_state)
        button = tk.Button(text="Show message", font=("Arial", 18), command=self.message)

        label.pack(padx=10, pady=10)
        self.textbox.pack(padx=10, pady=10)
        check.pack(padx=10, pady=10)
        button.pack(padx=10, pady=10)

    def message(self):
        self.textbox.insert(tk.END, str(self.check_state.get()))


root = tk.Tk()
MyGui(root).pack()
root.mainloop()

class MyGui(tk.Frame):

    def __init__(self, parent):
        super().__init__(parent)
        label = tk.Label(text="My message", font=("Arial", 18))
        self.textbox = tk.Text(height=3)
        self.check_state = tk.IntVar()
        check = tk.Checkbutton(text="State check", font=("Arial", 16), variable=self.check_state)
        button = tk.Button(text="Show message", font=("Arial", 18), command=self.message)

        label.pack(padx=10, pady=10)
        self.textbox.pack(padx=10, pady=10)
        check.pack(padx=10, pady=10)
        button.pack(padx=10, pady=10)

    def message(self):
        self.textbox.insert(tk.END, str(self.check_state.get()))


root = tk.Tk()
MyGui(root).pack()
root.mainloop()
This looks like a more complicated version than the Window version, but there are benefits that come with the extra complexity. Now MyGui can be the view in the root window, or it can be in a toplevel window, or it can be in a dialog, or it can be in a window with other views, or it can be one of many views in a notebook. That's a lot of flexibility for very little extra code.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] main GUI is not shown flash77 3 747 Feb-20-2024, 05:27 PM
Last Post: flash77
  Errors while running dash plotly code Nischitha 3 5,399 Aug-24-2017, 10:54 AM
Last Post: Larz60+
  which button should be shown gray 1 3,234 Mar-30-2017, 11:57 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020