Posts: 3
Threads: 1
Joined: Jun 2024
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
Posts: 1,145
Threads: 114
Joined: Sep 2019
Jun-28-2024, 08:34 PM
(This post was last modified: Jun-28-2024, 08:34 PM by menator01.)
Look at the __init__
you have __int__
Posts: 6,792
Threads: 20
Joined: Feb 2020
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.
Posts: 3
Threads: 1
Joined: Jun 2024
Jul-01-2024, 10:58 AM
(This post was last modified: Jul-01-2024, 10:59 AM by INS.)
(Jun-28-2024, 08:34 PM)menator01 Wrote: Look at the __init__
you have __int__
It's ok now, Thanks a lot.
Posts: 3
Threads: 1
Joined: Jun 2024
(Jun-28-2024, 09:17 PM)deanhystad Wrote: 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.
I'm glad for this short and quick lecture on my program. As I'm still a beginner, my location doesn't give much to finding a more reliable and qualitative learning source. I had to go through a lot just to find better materials.
Anyway, Thanks a lot.
|