Hack Forums
[Python] Tkinter program with classes(Object oriented) - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] Tkinter program with classes(Object oriented) (/showthread.php?tid=5825780)



Tkinter program with classes(Object oriented) - CrazyLinuxHacker - Apr 16, 2018

Code
# Modules
from tkinter import *

# Converter function

def converter():
    try:
        c = float(entry1.get())
        fahrenheit = c * 9 / 5 + 32
        entry2.delete(0, END)
        entry2.insert(0, fahrenheit)
    except:
        print("Celcius field is empty!")

# Create window

window = Tk()
window.title("Celsius to Fahrenheit converter")
window.geometry("250x100")

# First label

label1 = Label(window, text='Celsius')
label1.grid(row=0, sticky=W)

# First entry

entry1 = Entry(window)
entry1.grid(row=0, column=1)

# Second label

label2 = Label(window, text='Fahrenheit')
label2.grid(row=1, sticky=W)

# Second entry

entry2 = Entry(window)
entry2.grid(row=1, column=1)

# Button
button1 = Button(window, text='Convert', command=converter)
button1.grid(row=2, sticky=W)

window.mainloop()
How can i made the same program with classes? 🤨


RE: Tkinter program with classes(Object oriented) - .kr. - Apr 16, 2018

try it like this:
GUI.py:
Code
from tkinter import *

from Converter import Converter

class GUI:
    def __init__(self):
        self.window = Tk()
        self.window.title("Celsius to Fahrenheit converter")
        self.window.geometry("250x100")

        self.label1 = Label(self.window, text='Celsius')
        self.label1.grid(row=0, sticky=W)

        self.entry1 = Entry(self.window)
        self.entry1.grid(row=0, column=1)

        self.label2 = Label(self.window, text='Fahrenheit')
        self.label2.grid(row=1, sticky=W)

        self.entry2 = Entry(self.window)
        self.entry2.grid(row=1, column=1)

        self.button1 = Button(self.window, text='Convert', command=self.convert)
        self.button1.grid(row=2, sticky=W)

        self.window.mainloop()

    def convert(self):
        self.entry2.delete(0, END)
        self.entry2.insert(0, str(Converter().c2f(self.entry1.get())))

Converter.py:
Code
class Converter:
    def __init__(self):
        pass
    @staticmethod
    def c2f(c):
        try:
            return float(c) * 9 / 5 + 32
        except ValueError as e:
            return e

run.py:
Code
from GUI import GUI


def main():
    gui = GUI()

if __name__ == '__main__':
    main()



RE: Tkinter program with classes(Object oriented) - CrazyLinuxHacker - Apr 17, 2018

Thank you for your help but can you write it all together?


RE: Tkinter program with classes(Object oriented) - .kr. - Apr 17, 2018

are you serious?

Code
from tkinter import *


class Converter:
    def __init__(self):
        pass
    
    @staticmethod
    def c2f(c):
        try:
            return float(c) * 9 / 5 + 32
        except ValueError as e:
            return e

class GUI:
    def __init__(self):
        self.window = Tk()
        self.window.title("Celsius to Fahrenheit converter")
        self.window.geometry("250x100")

        self.label1 = Label(self.window, text='Celsius')
        self.label1.grid(row=0, sticky=W)

        self.entry1 = Entry(self.window)
        self.entry1.grid(row=0, column=1)

        self.label2 = Label(self.window, text='Fahrenheit')
        self.label2.grid(row=1, sticky=W)

        self.entry2 = Entry(self.window)
        self.entry2.grid(row=1, column=1)

        self.button1 = Button(self.window, text='Convert', command=self.convert)
        self.button1.grid(row=2, sticky=W)

        self.window.mainloop()

    def convert(self):
        self.entry2.delete(0, END)
        self.entry2.insert(0, str(Converter().c2f(self.entry1.get())))
        
        
def main():
    gui = GUI()

if __name__ == '__main__':
    main()



RE: Tkinter program with classes(Object oriented) - CrazyLinuxHacker - Apr 17, 2018

Thank you again. I'm new in programming.