0

I am trying to create a GUI for a company's online rental equipment. It should have a starting page with a title and 5 buttons for various categories of rentals. The user clicks a button, a new page opens with a title, body of text, and a check box to create an order form. The issue I am currently facing is how to get the buttons to produce a unique page that I can edit. It presently makes a new window but I'm not sure how to add text to it? I am pretty much a complete novice, so please keep that in mind. Here's the code:

from tkinter import *
Window = Tk()

def Open():
    New_Window = Tk()
    lbl = Label(New_Window, text="Water Damage Equipment")
    New_Window.mainloop()

# buttons for rental options 
Btn1 = Button(text="Extractors/Cleaners", command=Open)
Btn1.pack()
Btn1 = Button(text="Air Movers/Fans", command=Open)
Btn1.pack()
Btn1 = Button(text="Dehumidifers", command=Open)
Btn1.pack()
Btn1 = Button(text="Air Filtration", command=Open)
Btn1.pack()
Btn1 = Button(text="Generators", command=Open)
Btn1.pack()

Window.mainloop()


root = Tk()

def command():
    Toplevel(root)

button = Button(root, text="4 Dry Out e-Rental", command=command)
button.pack()

root.mainloop()
Lady
  • 21
  • 9
  • It appears that you're using a third-party module (`breezypythongui `) to do things, so I suggest you study its documentation and any examples therein to answer your own question. – martineau Oct 02 '21 at 20:07
  • From its documentation I don't think `breezypythongui` supports multiple windows because its `addButton()`, `addLabel()`, etc… methods don't have an argument specifying which window or `Frame` is their "master" like their `tkinter` counterparts do. It also appears that you are trying to create a separate `Tk` instances for each window, while that's possible, it's also problematic — see [Why are multiple instances of Tk discouraged?](https://stackoverflow.com/questions/48045401/why-are-multiple-instances-of-tk-discouraged). The better way IMO is by creating separate `Toplevel` widgets. – martineau Oct 02 '21 at 20:39
  • Here's a little documentation on [`Toplevel`](https://web.archive.org/web/20201111195659id_/http://effbot.org/tkinterbook/toplevel.htm) widgets. – martineau Oct 02 '21 at 20:40
  • I peeked at the `breezypythongui` source code and it would *not* be easy to modify it to support multiple windows. If you're trying to avoid learning how to use `tkinter` directly because you feel it's too complex or too poorly documented, there's another simplified module based on it I know about named [`graphics.py`](https://mcsp.wartburg.edu/zelle/python/) by Prof. John Zelle that *does* support multiple windows and has documention — there's even a book if you're interested. You can search for questions tagged "[zelle-graphics]" on this site to get an idea of what using it looks like. – martineau Oct 02 '21 at 22:47
  • @martineau I have been looking into Tkinter and Toplevel widgets since I read your comment-- thank you for mentioning them. I know it's very popular so would be valuable to learn, and believe I will be able to rewrite the program with some reference and reading more tutorials/examples. Would you give any advice in working on this program with Tkinter/Toplevel? – Lady Oct 02 '21 at 23:05
  • Well, maybe a little: After creating a new `Toplevel` window widget you can pass it as the first argument named `master` when creating a `tkinter.Button`, `tkinter.Label`, etc to make them be part of and appear on it. Also looks like you'll need a different `Open` function for each type of equipment *or* a single one that is passed an argument value specifying which type. Lastly, here are two sites I think are good tkinter *references*: [site 1](https://anzeljg.github.io/rin2/book2/2405/docs/tkinter/index.html) and [site 2](https://www.tutorialspoint.com/python/python_gui_programming.htm). – martineau Oct 02 '21 at 23:22

1 Answers1

0

Working with tkinter can be tricky. I built a library called easy_gui to wrap tkinter and simplify this sort of project; you might find it handy here.

Check out the below code for an example that sets up most of the functionality you describe. To actually "create an order form", you'll probably need a lot more going on in the create_order function.

Each of the buttons in the main window needs a command_func function that gets triggered when clicked. I recommend adding methods to the GUI class like extractors_cleaners to handle each of the buttons separately.

Within the extractors_cleaners method, creating a new window is completely handled by the with self.popup() as popup: line. Just add widgets to popup as needed to manipulate the new window. The code gets a little dense, but the "Create Order" button in the popup window calls the create_order function and passes in text that we generate from within the new window.

import easy_gui


def create_order(text):
    with open('order_form.txt', 'w') as f:
        f.write(text)


class GUI(easy_gui.EasyGUI):
    def __init__(self):
        self.title('Order Forms')  # set title of GUI window
        self.add_widget('button', 'Extractors/Cleaners', command_func=self.extractors_cleaners)
        self.add_widget('button', 'Air Movers/Fans', command_func=self.air_movers)
        self.add_widget('button', 'Dehumidifiers', command_func=self.dehumidifiers)
        self.add_widget('button', 'Air Filtration', command_func=lambda *args: print('...'))
        self.add_widget('button', 'Generators', command_func=lambda *args: print('...'))

    def extractors_cleaners(self, *args):  # gets called when associated button is clicked
        with self.popup() as popup:  # generate a popup window
            popup.geometry('500x600')  # size of popup window
            popup.add_widget('label', 'Extractors/Cleaners Order Form', underline=True, bold=True)
            popup.add_widget('label', 'Order Details...\nMore Details...')
            text = popup.add_widget('scrolledtext')
            checkbox = popup.add_widget('checkbox', 'Fast Order?')

            def order_text():  # read status of 'text' and 'checkbox' widgets and return a string
                return '---FAST ORDER---\n' + '\n'.join(text.get()) if checkbox.get() else '\n'.join(text.get())
            popup.add_widget('button', 'Create Order', command_func=lambda *args: create_order(order_text()))

    def air_movers(self, *args):
        ...

    def dehumidifiers(self, *args):
        ...


GUI()

Unfortunately there's not much documentation for easy_gui currently, but there are a few more examples here: https://github.com/zachbateman/easy_gui

  • Not the most [original name](https://pypi.org/project/easygui/). – martineau Oct 03 '21 at 08:05
  • Perhaps not, but hopefully the code is more useful than the name is original. – Zach Bateman Oct 04 '21 at 15:23
  • @Zach Bateman It says " ModuleNotFoundError: No module named 'easy_gui' " How do I import this? – Lady Oct 06 '21 at 21:46
  • @Lady You'll need to install the package as it's not in the Python standard library. Try running `pip install easy_gui` in a command window. After it is installed, the `import easy_gui` line in your code should work. Here's more info on using `pip`: https://docs.python.org/3/installing/index.html – Zach Bateman Oct 07 '21 at 12:52
  • I have tried installing such, however am receiving this code in my terminal "-bash: pip: command not found". I am on a Chromebook if that is the issue? But Linux is run alongside my Chrome OS. – Lady Oct 07 '21 at 20:14
  • Do you know which version of Python you are running? `pip` usually comes installed with Python on recent versions. Perhaps you could try installing Python 3.7 or higher from www.python.org? Outside of using pip, you could download the easy_gui code itself from here: https://github.com/zachbateman/easy_gui and put the "easy_gui" folder inside the same folder as your GUI code. – Zach Bateman Oct 09 '21 at 21:43