I'm working in Python 3.5 and TKinteer. Inside of a text widget, I have created a context menu that appears when the user right-clicks. However, when I try and create the commands I want (cut, copy, paste), the commands seem to have no effect.
The relevant code is as follows:
from tkinter import *
class Application:
def __init__(self,master):
self.master = master
self.initUI()
def initUI(self):
root.title("Simple Text Editor")
scrollBar = Scrollbar(root)
self.textPad = Text(root, width=100, height=100, wrap='word',
yscrollcommand=scrollBar.set,
borderwidth=0, highlightthickness=0)
scrollBar.config(command=self.textPad.yview)
scrollBar.pack(side='right', fill='y')
self.textPad.pack(side='left', fill='both', expand=True)
class PopupMenu:
def __init__(self, master, *args, **kwargs):
self.popup_menu = Menu(root, tearoff=0)
self.popup_menu.add_command(label="Cut",
command=lambda: app.textPad.event_generate('<Control-x>'))
self.popup_menu.add_command(label="Copy",
command=lambda: app.textPad.event_generate('<Control-c>'))
self.popup_menu.add_command(label="Paste",
command=lambda: app.textPad.event_generate('<Control-v>'))
app.textPad.bind("<Button-3>", self.popup)
self.popup_menu.bind("<FocusOut>",self.popupFocusOut)
def popup(self, event):
self.popup_menu.post(event.x_root, event.y_root)
self.popup_menu.focus_set()
def popupFocusOut(self, event=None):
self.popup_menu.unpost()
root = Tk()
app = Application(root)
popupMenu = PopupMenu(root)
root.mainloop()
You don't want to generate <Control-x>, etc. Instead, generate the virtual events <<Cut>>, <<Copy>> and <<Paste>>.
Related
When attempting to create a second Toplevel in Tkinter after closing the first I get the error:
_tkinter.TclError: bad window path name ".!toplevel
The error only occurs when the first Toplevel is closed, when I run the code without close_window() no error occurs and new_window works and creates the second Toplevel. I need to be able to close the first Toplevel and am not sure what is going wrong here so any help is much appreciated.
Here is a minimal reproducible example.
import tkinter as tk
class auto_haven:
def __init__(self, master):
self.master = master
self.frame = tk.Frame(self.master)
self.frame.place(relwidth=1, relheight=1)
self.admin_login_button = tk.Button(self.frame, text="Admin Login", font=40, command=self.new_window)
self.admin_login_button.place(relwidth=1, relheight=1)
def new_window(self):
self.newWindow = tk.Toplevel(self.master)
self.app = admin_login(self.newWindow)
class admin_login:
def __init__(self, master):
self.master = master
self.frame = tk.Frame(self.master)
self.frame.place(relwidth=1, relheight=1)
self.login_button = tk.Button(self.frame, text="Login", font=40, command=self.login)
self.login_button.pack()
self.back_button = tk.Button(self.frame, text="Exit", font=40, command=self.close_window)
self.back_button.pack()
def new_window(self):
self.newWindow = tk.Toplevel(self.master)
self.app = admin_panel(self.newWindow)
def close_window(self):
self.master.destroy()
def login(self):
self.close_window()
self.new_window()
class admin_panel:
def __init__(self, master):
self.master = master
self.frame = tk.Frame(self.master)
self.quitButton = tk.Button(self.frame, text = 'Quit', width = 25, command = self.close_window)
self.quitButton.pack()
self.frame.pack()
def close_window(self):
self.master.destroy()
def main():
root = tk.Tk()
app = auto_haven(root)
root.mainloop()
if __name__ == '__main__':
main()
When you call self.login, the first thing it does is call self.close_window(). When you do that, it calls self.master.destroy(). It then calls self.new_window() which calls self.newWindow = tk.Toplevel(self.master).
Notice that you are now trying to create a new window as a child of self.master, but you've destroyed self.master so tkinter will throw an error. When you create a new window, it needs to be the child of an existing window, such as the root window.
I am creating a simple application in python using tkinter, which includes a menu from which I can pick different options and move to new menus. I am using the answer given by Steven Vascellaro here to destroy the frames as I move between them. In an earlier test version of the program I was able to give the buttons my custom font and have it display correctly, but when I add the
master, which switches between the different frames, the font no longer works, only making the text on the buttons slightly larger.
The version of the code that works correctly is this:
import tkinter as tk
from tkinter.font import Font, nametofont
class MainMenu(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.grid()
self.createWidgets()
def createWidgets(self):
global myFont
top=self.winfo_toplevel()
top.rowconfigure(0, weight=1)
top.columnconfigure(0, weight=1)
self.rowconfigure(0, weight=1, pad=50)
self.columnconfigure(0, weight=1)
self.resume = tk.Button(self, text='Continue', height=2, width=10, font=myFont, command=self.quit)
self.library = tk.Button(self, text='Library', height=2, width=10, command=self.quit)
self.resume.grid(row=1, column=0,sticky=tk.N+tk.E+tk.W)
self.library.grid(row=3, column=0,sticky=tk.E+tk.W)
root = tk.Tk()
global myFont
fontCheck = open("Options.txt","r")
for line in fontCheck:
if "Font" in line:
tempLine = line.strip()
fontDetails = tempLine.split(",")
print(fontDetails)
myFont = Font(family=fontDetails[1], size=int(fontDetails[2]), weight="bold")
app = MainMenu()
app.mainloop()
root.destroy() `
Producing a menu that looks like this
But when I add this master section it no longer works:
import tkinter as tk
from tkinter.font import Font, nametofont
class Application(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self._frame = None
self.switch_frame(MainMenu)
def switch_frame(self, frame_class):
new_frame = frame_class(self)
if self._frame is not None:
self._frame.destroy()
self._frame = new_frame
self._frame.grid()
class MainMenu(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.grid()
self.createWidgets()
def createWidgets(self):
global myFont
top=self.winfo_toplevel()
top.rowconfigure(0, weight=1)
top.columnconfigure(0, weight=1)
self.rowconfigure(0, weight=1, pad=50)
self.columnconfigure(0, weight=1)
self.resume = tk.Button(self, text='Continue', height=2, width=10, font=myFont, command=self.quit)
self.library = tk.Button(self, text='Library', height=2, width=10, command=self.quit)
self.resume.grid(row=1, column=0,sticky=tk.N+tk.E+tk.W)
self.library.grid(row=3, column=0,sticky=tk.E+tk.W)
root = tk.Tk()
global myFont
fontCheck = open("Options.txt","r")
for line in fontCheck:
if "Font" in line:
tempLine = line.strip()
fontDetails = tempLine.split(",")
print(fontDetails)
myFont = Font(family=fontDetails[1], size=int(fontDetails[2]), weight="bold")
app = Application()
app.mainloop()
root.destroy()
It creates a menu that looks like this
I would love if someone could explain why the font is not working correctly across the frames and explain how I can fix this issue.
You're creating 2 instances of tk.Tk: One where you set up the font, and one for your application. These two instances don't share the font. The solution would be to set up the Font inside your Application class (as a method, probably, and at initialization most likely).
class Application(tk.Tk):
def __init__(self, *args, fontfile = None, **kw):
super().__init__(*args, **kw)
if fontfile is None: fontfile = "Options.txt"
self._frame = None
self.fontfile = fontfile
self.setupFont()
self.switch_frame(MainMenu)
def setupFont(self):
global myFont
with open(self.fontfile,"r")
for line in fontCheck:
if "Font" in line:
tempLine = line.strip()
fontDetails = tempLine.split(",")
print(fontDetails)
myFont = Font(family=fontDetails[1], size=int(fontDetails[2]), weight="bold")
A few other Notes:
I don't particularly like the use of global; either having it as an attribute of Application or as a ttk.Style would be preferable (in my opinion).
You may want to consider using a options file with a predefined structure (recommend json) that can be read from in a more explicit fashion.
I want to be able to adjust the color of a ttk.Frame() widget. I know this question has been asked numerous times here. A very useful answer was given here: Change color of "tab header" in ttk.Notebook. One can do this using style.theme_create().
I want to make a similar adjustment using ttk.Style() but cannot figure out how. Could someone explain to me how I can do this?
The reason I am not using the method proposed in the above link is because there are other widgets in my code that I want to (and can) customize using ttk.Style().
import tkinter as tk
from tkinter import ttk
class Window():
def __init__(self, parent):
self.parent = parent
self.parent.minsize(width=600, height=400)
self.widgets()
def widgets(self):
s1 = ttk.Style()
s1.configure('My.TNotebook.Tab', padding=5, background='red')
s1.map('My.TNotebook.Tab', background=[('selected', 'green')])
self.nb1 = ttk.Notebook(self.parent, style='My.TNotebook.Tab')
self.tab1 = ttk.Frame(self.nb1)
self.tab2 = ttk.Frame(self.nb1)
self.tab3 = ttk.Frame(self.nb1)
self.nb1.add(self.tab1, text='Tab1')
self.nb1.add(self.tab2, text='Tab2')
self.nb1.add(self.tab3, text='Tab3')
self.nb1.place(relx=0.1, rely=0.1, width=500, height=200)
self.b1 = ttk.Button(self.parent, text='Quit', command=self.quit)
self.b1.place(relx=0.4, rely=0.7, height=70, width=150)
def quit(self):
self.parent.destroy()
root = tk.Tk()
app = Window(root)
root.mainloop()
I searched more and found an interesting and simple solution here: http://page.sourceforge.net/html/themes.html. The key to solving the problem seems to be the need to tell Python which theme to use (I am running Python on Windows). In my answer, I use classic but winnative, clam, alt, and default will do too. Using vista or xpnative makes no effect on the tabs' colors.
import tkinter as tk
from tkinter import ttk
class Window():
def __init__(self, parent):
self.parent = parent
self.parent.minsize(width=600, height=400)
self.widgets()
def widgets(self):
s1 = ttk.Style()
s1.theme_use('classic')
s1.configure('TNotebook.Tab', background='navajo white')
s1.map('TNotebook.Tab', background=[('selected', 'goldenrod'), ('active', 'goldenrod')])
self.nb1 = ttk.Notebook(self.parent)
self.nb1.place(relx=0.1, rely=0.1, width=500, height=200)
self.tab1 = ttk.Frame(self.nb1)
self.tab2 = ttk.Frame(self.nb1)
self.tab3 = ttk.Frame(self.nb1)
self.nb1.add(self.tab1, text='Tab1')
self.nb1.add(self.tab2, text='Tab2')
self.nb1.add(self.tab3, text='Tab3')
self.b1 = ttk.Button(self.parent, text='Quit', command=self.quit)
self.b1.place(relx=0.4, rely=0.7, height=70, width=150)
def quit(self):
self.parent.destroy()
root = tk.Tk()
app = Window(root)
root.mainloop()
I've looked at other question with a similar title and have read the answers, however nothing has worked for me. I am trying to make a simple app with a listbox + scroll bar with two buttons below it all within a group box. I've used pyqt but this is my first time using tkinter:
import tkinter as tk
class InputWindow(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
self.parent = parent
self.initialize()
def initialize(self):
# Group box to contain the widgets
self.input = tk.LabelFrame(self, text="Input Files")
# Listbox with scrollbar to the side
self.listbox = tk.Listbox(self.input)
self.scrollbar = tk.Scrollbar(self.listbox, orient=tk.VERTICAL)
self.listbox.config(yscrollcommand=self.scrollbar.set)
self.scrollbar.config(command=self.listbox.yview)
self.listbox.grid(row=0, column=0, columnspan=2)
self.add_btn = tk.Button(self.input, text="Add...")
self.add_btn.grid(row=1, column=0)
self.remove_btn = tk.Button(self.input, text="Remove")
self.remove_btn.grid(row=1, column=1)
if __name__ == "__main__":
root = tk.Tk()
app = InputWindow(root)
root.mainloop()
This is more or less what I want but in tkinter:
What am I doing wrong/how can this be done?
You're forgetting two things:
To pack (or grid or place) app
To pack (or grid or place) input
You're program with the required statements:
import tkinter as tk
class InputWindow(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
self.parent = parent
self.initialize()
def initialize(self):
# Group box to contain the widgets
self.input = tk.LabelFrame(self, text="Input Files")
# Listbox with scrollbar to the side
self.listbox = tk.Listbox(self.input)
self.scrollbar = tk.Scrollbar(self.listbox, orient=tk.VERTICAL)
self.listbox.config(yscrollcommand=self.scrollbar.set)
self.scrollbar.config(command=self.listbox.yview)
self.listbox.grid(row=0, column=0, columnspan=2)
self.add_btn = tk.Button(self.input, text="Add...")
self.add_btn.grid(row=1, column=0)
self.remove_btn = tk.Button(self.input, text="Remove")
self.remove_btn.grid(row=1, column=1)
self.input.pack(expand=1, fill="both") # Do not forget to pack!
if __name__ == "__main__":
root = tk.Tk()
app = InputWindow(root)
app.pack(expand=1, fill="both") # packing!
root.mainloop()
This is the front end I developed for my application using Tkinter:
from Tkinter import *
class Example(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def initUI(self):
self.parent.title("Simple")
self.pack(fill=BOTH, expand=1)
frame = Frame(self, relief="flat", borderwidth=1)
label=Label(frame,text="Scope:")
label.pack(side="left", fill=None, expand=False)
var = StringVar()
var.set("today")
list = OptionMenu(frame, var, "today","yesterday","this week","last week","this month","last month")
list.pack(side="left", fill=None, expand=False)
fetchButton = Button(frame, text="Fetch",command=self.handle(var))
fetchButton.pack(side="left", fill=None, expand=False)
frame.grid(row=1,column=1,pady=4,padx=5,sticky=W)
area = Text(self,height=15,width=60)
area.grid(row=2,column=1,rowspan=1,pady=4,padx=5)
scroll = Scrollbar(self)
scroll.pack(side=RIGHT, fill=Y)
area.config(yscrollcommand=scroll.set)
scroll.config(command=area.yview)
scroll.grid(row=2, column=2, sticky='nsew')
quitButton = Button(self, text="Cancel",command=self.quit)
quitButton.grid(pady=4,padx=5,sticky=W,row=3, column=1)
root = Tk()
app = Example(root)
root.mainloop()
Where exactly do I have to put the handle() method so it can write repeatedly to the text widget? When I put handle() within the Example class and use self.area.insert(), it shows an error saying
Example instance has no attribute 'area'
Please help out.
You need to pass the function object to the Button instance, not a function call. i.e.
fetchButton = Button(frame, text="Fetch",command=self.handle)
To make the handle work in the context of the rest of the code:
from Tkinter import *
class Example(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.parent.title("Simple")
self.pack(fill=BOTH, expand=1)
self.init_ui()
def init_ui(self):
self.frame = Frame(self, relief="flat", borderwidth=1)
self.frame.grid(row=1,column=1,pady=4,padx=5,sticky=W)
self.label=Label(self.frame,text="Scope:")
self.label.pack(side="left", fill=None, expand=False)
self.var = StringVar()
self.var.set("today")
self.list = OptionMenu(self.frame, self.var, "today","yesterday",
"this week","last week","this month",
"last month")
self.list.pack(side="left", fill=None, expand=False)
self.fetchButton = Button(self.frame, text="Fetch",command=self.handle)
self.fetchButton.pack(side="left", fill=None, expand=False)
self.area = Text(self,height=15,width=60)
self.area.grid(row=2,column=1,rowspan=1,pady=4,padx=5)
self.scroll = Scrollbar(self)
self.scroll.pack(side=RIGHT, fill=Y)
self.area.config(yscrollcommand=self.scroll.set)
self.scroll.config(command=self.area.yview)
self.scroll.grid(row=2, column=2, sticky='nsew')
self.quitButton = Button(self, text="Cancel",command=self.quit)
self.quitButton.grid(pady=4,padx=5,sticky=W,row=3, column=1)
def handle(self):
self.area.delete(1.0, END)
self.area.insert(CURRENT,self.var.get())
if __name__ == "__main__":
root = Tk()
app = Example(root)
root.mainloop()
Declaring your widgets as attributes will save you a lot of pain an suffering as your application expands. Also keeping references to everything in Tk can stop some unwanted garbage collection, particularly with images in Label instances.
It is also worth noting that using grid and pack interchangeably is likely to lead to bugs later on.