-1

I am trying to create a basic invoicing system. However i have encountered an issue as you can tell from my the title, is there any way to achieve this. I have been using a counter to determine if the window should open or not but i dont think it is right.

from tkinter import *

window = Tk()
count = 0


def openNewWindow():
    global count

    count = count + 1

    if count == 1:

        newWindow = Toplevel(window)
        newWindow.title("New Window")
        newWindow.geometry("800x800")
        newWindow.title('test ©')  # Frame title
        newWindow.iconbitmap('icon4.ico')  # Frame logo
        

    if 'normal' == newWindow.state():
        count = 2

    else:
        count = 0


width = window.winfo_screenwidth()
height = window.winfo_screenheight()
window.geometry("%dx%d" % (width, height))

bg = PhotoImage(file="bsor.gif")
label_image = Label(window, image=bg)
label_image.place(x=0, y=0)

title_label = Label(window, text="Job Management System", bg="black", fg="white")
title_label.config(font=("Courier", 70))
title_label.place(x=65, y=3)

customer_database_button = Button(window, text="Customer Database", width="23", height="2", 
font=('Courier', 13, 'bold'), command=openNewWindow)
customer_database_button.grid(row=3, column=0, pady=185, padx=(110, 0))

employee_database_button = Button(window, text="Employee Database", width="23", height="2",
                              font=('Courier', 13, 'bold'))
employee_database_button.grid(row=3, column=1, pady=10, padx=(50, 0))

job_category_button = Button(window, text="Job Category (Pricing)", width="23", height="2",
                         font=('Courier', 13, 'bold'))
job_category_button.grid(row=3, column=2, pady=10, padx=(50, 0))

quote_sale_button = Button(window, text="Quotes / Sales", width="23", height="2", font= 
('Courier', 13, 'bold'))
quote_sale_button.grid(row=3, column=3, pady=10, padx=(50, 0))

cash_management_button = Button(window, text="Cash Management", width="23", height="2", font= 
('Courier', 13, 'bold'))
cash_management_button.grid(row=3, column=4, pady=10, padx=(50, 0))

analysis_mode_button = Button(window, text="Analysis Mode", width="23", height="2", font= 
('Courier', 13, 'bold'))
analysis_mode_button.grid(row=3, column=5, pady=10, padx=(50, 0))

window.title('test')  # Frame title
window.iconbitmap('icon4.ico')  # Frame logo

window.mainloop()
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • just disable the button on opening and enable on closing or bind the window to focus out event so that it gets automatically closed (not preferred if some important stuff is being typed in it), also you should provide a [mre] and don't use `*` when importing, import only what you need – Matiiss Oct 14 '21 at 20:16
  • thanks you much appreciated! – RazzcoSolutions Oct 14 '21 at 20:23

1 Answers1

0

Here is a minimal example on how to do it (works best with only one additional allowed window):

from tkinter import Tk, Toplevel, Button


def open_window(button):
    button.config(state='disabled')
    top = Toplevel(root)
    top.transient(root)
    top.focus_set()
    top.bind('<Destroy>', lambda _: btn.config(state='normal'))


root = Tk()
root.geometry('300x200')

btn = Button(root, text='Open new window!', command=lambda: open_window(btn))
btn.pack(expand=True)

root.mainloop()

Just have the function disable the button and bind a <Destroy> event to the Toplevel to set the button's state back to normal. (Also you may want to use .transient on the Toplevel to make it appear above its master so that people don't forget that they haven't closed the window and wonder why they can't press the button (it will also not display additional icon in the taskbar))

Also:
I strongly advise against using wildcard (*) when importing something, You should either import what You need, e.g. from module import Class1, func_1, var_2 and so on or import the whole module: import module then You can also use an alias: import module as md or sth like that, the point is that don't import everything unless You actually know what You are doing; name clashes are the issue.

I strongly suggest following PEP 8 - Style Guide for Python Code. Function and variable names should be in snake_case, class names in CapitalCase. Don't have space around = if it is used as a part of keyword argument (func(arg='value')) but have space around = if it is used for assigning a value (variable = 'some value'). Have space around operators (+-/ etc.: value = x + y(except here value += x + y)). Have two blank lines around function and class declarations.

Matiiss
  • 5,970
  • 2
  • 12
  • 29
  • Thank you so much, this helps a lot! – RazzcoSolutions Oct 14 '21 at 21:43
  • @RazzcoSolutions Please see [What to do when someone answers my question?](https://stackoverflow.com/help/someone-answers) and [How does accepting an answer work?](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work), this will help future readers and also allow to mark other questions as duplicates. Thank you! – Matiiss Oct 14 '21 at 21:48