4
\$\begingroup\$

I've been practising trying to write my code neater, so decided to build a practice GUI, the code works, However, how could I tidy this up in terms of separating out the different parts of the GUI, such as a separate defs for labels, Combobox etc? or using a function for the 'with open' section.

from tkinter import*
from tkinter.ttk import *

root = Tk()

class GUI:
    def __init__(self, master):
        self.master = master
        master.title('PATM Generator')
        master.geometry('+600+300')

        #Label1
        master.label1 = Label(root, text = 'Select test bed:')
        master.label1.grid(row = 0, column = 0, padx = 10, pady = 5)

        #Combobox
        master.combo = Combobox(root)
        master.combo.grid(row =1, column = 0)
        master.combo['values'] = (TB)

        #Label2
        master.label2 = Label(root, text = 'Enter TRD index:')
        master.label2.grid(row = 2, column = 0, padx = 10, pady = 5)

        #Entry
        master.entry = Entry(root)
        master.entry.grid(row = 3, column = 0, padx = 10, pady = 0)

        #Button
        master.button = Button(root, text = 'Append to txt')
        master.button.grid(row = 4, padx = 10, pady = 5)

with open('TB.txt') as inFile:
    TB = [line for line in inFile]

def main():
    GUI(root)
    root.mainloop()

main()
\$\endgroup\$
3
  • \$\begingroup\$ the comments do not add additional information, I would just remove them. \$\endgroup\$
    – bb1950328
    Commented Apr 27, 2021 at 10:09
  • 1
    \$\begingroup\$ Welcome to Code Review! What task does this code accomplish? Please tell us, and also make that the title of the question via edit. Maybe you missed the placeholder on the title element: "State the task that your code accomplishes. Make your title distinctive.". Also from How to Ask: "State what your code does in your title, not your main concerns about it.". e.g. PATM generator? \$\endgroup\$ Commented Apr 29, 2021 at 6:19
  • \$\begingroup\$ I edited the title so it describes the code. Feel free to edit it if you feel there is a more appropriate title. \$\endgroup\$ Commented May 2, 2021 at 17:32

2 Answers 2

1
\$\begingroup\$

You didn't give an example of your text file, maybe that would shed some light on the use of a combo box instead of a text widget. You started out with using self in your class but soon used master.

master.combo = Combobox(root)

Using self. prefix allows you to access the objects, widgets and any other data in other functions.

apple
orange
banana
grape
grapefruit
tangerine

combo_stuff.txt

So I'm guessing that you want to add whatever is typed into the entry widget to your text file- Here's one way to accomplish it.

from tkinter import*
from tkinter.ttk import *



class GUI:
    def __init__(self, master):
        self.master = master
        self.master.title('PATM Generator')
        self.master.geometry('+600+300')

        #Label1
        self.label1 = Label(root, text = 'Select test bed')  
        self.label1.grid(row = 0, column = 0, padx = 10, pady = 5)

        #Combobox
        self.combo = Combobox(root)
        self.combo.grid(row =1, column = 0)
        self.combo['values'] = (TB)

        #Label2
        self.label2 = Label(root, text = 'Enter TRD index:')
        self.label2.grid(row = 2, column = 0, padx = 10, pady = 5)

        #Entry
        self.entry = Entry(root)
        self.entry.grid(row = 3, column = 0, padx = 10, pady = 0)

        #Button
        self.button = Button(root, text = 'Append to txt',
                           command=self.append_text)
        self.button.grid(row = 4, padx = 10, pady = 5)
    def append_text(self):
        item= self.entry.get()
        if item: # insure there's text in the entry
            print(item)
        
            with open('combo_stuff.txt', mode='a',) as write_file:
                write_file.write('\n' + item)
                write_file.close()
            self.entry.delete(0,'end')
        

with open('combo_stuff.txt') as inFile:
    TB = [line for line in inFile]

if __name__ == '__main__':
    root = Tk()
    GUI(root)
    root.mainloop()

The easiest way to open a file is with the pre-made dialog. here's a link to some examples : tkinter file dialog

\$\endgroup\$
2
\$\begingroup\$
if __name__ == "__main__":
    GUI(root)
    root.mainloop()

instead of

def main():
    GUI(root)
    root.mainloop()

main()

it's the "pythonic" way of accomplishing what you've written.

\$\endgroup\$
1
  • \$\begingroup\$ Using main() and if __name__ == "__main__": is common as then you can make main a setuptool's console entrypoint and have the if handle python -m name invocation. \$\endgroup\$
    – Peilonrayz
    Commented May 5, 2021 at 8:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.