I am trying to make a GUI notepad using python ( tkinter ) But the problem is that the secondary GUI window opens up i.e the Listbox before the primary window Notepad
The secondary window should only open when the user clicks on the window menu and then on colour
The window menu has self.listbox.listbx command which I guess should only work when the user clicks on window menu, What am I doin' wrong?
from tkinter import *
from tkinter import messagebox as msg
from tkinter.filedialog import askopenfilename, asksaveasfilename
import os
class Notepad(Tk):
"""Class for notepad """
# Command for menus-
def newFile(self):
self.crfile = None
self.txtarea.delete(1.0, END)
def saveFile(self):
if self.crfile == None:
self.crfile = asksaveasfilename(initialfile="Untitled.txt",
defaultextension=".txt", filetypes=[("All Files", "*.*"), ("Text Documents", "*.txt")])
if self.crfile == "":
self.crfile = None
else:
# Save as new file
with open(self.crfile, "w") as f:
f.write(self.txtarea.get(1.0, END))
self.title(os.path.basename(
self.crfile + "- Notepad"))
else:
# Save the file
with open(self.crfile, "w") as f:
f.write(self.txtarea.get(1.0, END))
self.title(os.path.basename(
self.crfile + "- Notepad"))
def openFile(self):
self.crfile = askopenfilename(defaultextension=".txt", filetypes=[
("All Files", "*.*"), ("Text Documents", "*.txt")])
if self.crfile == "":
self.crfile = None
else:
self.title(os.path.basename(self.crfile) +
"-" + "Notepad")
self.txtarea.delete(1.0, END)
with open(self.crfile, "r") as f:
self.txtarea.insert(1.0, f.read())
# commands for edit menu-
def copy(self):
self.txtarea.event_generate(("<<Copy>>"))
def paste(self):
self.txtarea.event_generate(("<<Paste>>"))
def cut(self):
self.txtarea.event_generate(("<<Cut>>"))
# commands for help menu-
def About(self):
msg.showinfo("Notepad -Help",
"This is a simple notepad made by ...")
def menus(self):
# TODO Menus
mainmenu = Menu(self, tearoff="0")
# file menu
file = Menu(mainmenu, tearoff=0)
# Creating new file
file.add_command(label="New", command=self.newFile)
# Saving the current file
file.add_command(label="Save", command=self.saveFile)
# Opening a Pre-Existing file
file.add_command(label="Open", command=self.openFile)
file.add_separator()
file.add_command(label="Exit", command=self.destroy) # Exit command
mainmenu.add_cascade(menu=file, label="File")
self.config(menu=mainmenu)
# Edit menu---
edit = Menu(mainmenu, tearoff=0)
# To cut any part of the Textarea
edit.add_command(label="Cut", command=self.cut)
# To copy any part of the Textarea
edit.add_command(label="Copy", command=self.copy)
# To paste any cut or copied Text.
edit.add_command(label="Paste", command=self.paste)
mainmenu.add_cascade(menu=edit, label="Edit")
self.config(menu=mainmenu)
# Help menu---
helpm = Menu(mainmenu, tearoff=0)
# Displays about the notepad
helpm.add_command(label="About", command=self.About)
mainmenu.add_cascade(menu=helpm, label="Help")
self.config(menu=mainmenu)
# Window menu
win = Menu(mainmenu, tearoff=0)
win.add_command(label="Colour", command=self.listbox.listbx)
mainmenu.add_cascade(menu=win, label="Window")
self.config(menu=mainmenu)
def textwid(self):
# TODO Text widget---
self.txtarea = Text(self)
self.crfile = None
self.txtarea.pack(expand=True, fill=BOTH)
# TODO Scrollbar---
self.scbar = Scrollbar(self.txtarea)
self.txtarea.config(yscrollcommand=self.scbar.set)
self.scbar.config(command=self.txtarea.yview)
self.scbar.pack(side=RIGHT, fill=BOTH)
class listbox(Tk):
def listbx(self):
self.lbx = Listbox(self)
colours = ["blue", "red", "yellow",
"grey", "green", "light blue", "black"]
for c in colours:
self.lbx.insert(END, c)
self.lbx.pack()
self.lbx.bind("<<ListboxSelect>>", self.selection)
def selection(self, event):
"""Selection of colour from the lisbox above to apply changes"""
colour = self.lbx.get(ANCHOR)
msgselect = msg.askokcancel(
"Window-Colour", "Confirm the colour to apply to the window")
if msgselect == True:
root.configure(bg=colour)
self.destroy()
else:
self.destroy()
root2 = listbox()
root2.geometry("300x200")
root2.title("Window-Colour")
root2.listbx()
root2.mainloop()
if __name__ == "__main__":
# Basic tkinter startup
root = Notepad()
root.geometry("800x700")
root.title("Notepad-Untitled-1")
root.menus()
root.textwid()
root.wm_iconbitmap("noteicon.ico")
root.mainloop()
Thank you!
root2 = listbox()
gets executed unconditionally, as part of the creation of theNotepad
class. Note that having two separate windows that both derive fromTk
is going to cause all sorts of problems - you need to useToplevel()
instead for all windows after the original one. – jasonharper Oct 22 '20 at 18:09