Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python tkinter, how to disable background buttons from being clicked

When opening a new tkinter window, I only want the user to be able to click buttons on the new window. They should not be able to click on buttons from other windows that are part of the application. How would I accomplish this?

Here is a snip of my code:

def exportEFS(self):
  self.exportGUI = Toplevel()
  Button(self.exportGUI, text='Backup', command=self.backup).pack(padx=100,pady=5)
  Button(self.exportGUI, text='Restore', command=self.restore).pack(padx=100,pady=5)

def backup(self):
  self.backupWindow = Toplevel()

  message = "Enter a name for your Backup."

  Label(self.backupWindow, text=message).pack()

  self.entry = Entry(self.backupWindow,text="enter your choice")
  self.entry.pack(side=TOP,padx=10,pady=12)

  self.button = Button(self.backupWindow, text="Backup",command=self.backupCallBack)
  self.button.pack(side=BOTTOM,padx=10,pady=10)

In this snip, once the backupWindow is opened, the exportGUI remains open, but the user should not be able to click "Backup" or "Restore" while the backupWindow is opened.

Thanks!

like image 678
Brad Conyers Avatar asked Nov 24 '25 17:11

Brad Conyers


1 Answers

You will want to call grab_set on the TopLevel window so that all keyboard and mouse events are sent to that.

def exportEFS(self):
  self.exportGUI = Toplevel()
  Button(self.exportGUI, text='Backup', command=self.backup).pack(padx=100,pady=5)
  Button(self.exportGUI, text='Restore', command=self.restore).pack(padx=100,pady=5)

def backup(self):
  self.backupWindow = Toplevel()
  self.backupWindow.grab_set()

  message = "Enter a name for your Backup."

  Label(self.backupWindow, text=message).pack()

  self.entry = Entry(self.backupWindow,text="enter your choice")
  self.entry.pack(side=TOP,padx=10,pady=12)

  self.button = Button(self.backupWindow, text="Backup",command=self.backupCallBack)
  self.button.pack(side=BOTTOM,padx=10,pady=10)
like image 58
smont Avatar answered Nov 27 '25 06:11

smont



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!