python tkinter 右键菜单不显示怎么改
本帖最后由 HDL颌 于 2022-1-18 17:07 编辑from asyncio import events
import tkinter as tk
from tkinter import INSERT, LEFT, filedialog
from tkinter.filedialog import *
from tkinter.ttk import Treeview
from PIL import Image
class MyApp(tk.Tk):
def __init__(self):
super().__init__()
self.set_window(800, 500)
self.frame1 = tk.Frame().pack()
self.set_treeview()
self.set_text()
self.set_menu()
def set_window(win, width, height):
screenwidth = win.winfo_screenwidth()
screenheight = win.winfo_screenheight()
size = '%dx%d+%d+%d' % (width, height,
(screenwidth - width)/2, (screenheight - height)/2)
win.geometry(size)
def set_treeview(self):# 列表目录
tree = Treeview(self.frame1)
tree_1 = tree.insert("", 0, "0", text="目录", values=("0"))
tree_2 = tree.insert(tree_1, 0, "1", text="第一个", values=("1"))
tree_3 = tree.insert(tree_1, 0, "2", text="第二个", values=("2"))
tree.pack(side=LEFT, fill='y')
tree.bind("<Button-3>", self.popupmenu)
def set_text(self):# 文本编辑
global content_text
content_text = tk.Text(self.frame1, width=80, height=10, undo=True)
content_text.pack(side='left', fill='y', padx=10)
self.set_scrollbar(content_text)
def open_file(self, ):# 选择打开什么文件,返回文件名
filename = filedialog.askopenfilename()
f = open(filename, 'r')
f2 = f.read()
content_text.insert(INSERT, f2)
f.close()
def set_move(self):# 撤销
content_text.edit_undo()
pass
def save_file(self):# 保存content_text
filename = filedialog.asksaveasfilename()
with open(filename, 'w') as f:
f.write(content_text.get('1.0', tk.END))
def set_scrollbar(self, name):# 滚动条
self.name = name
VScroll1 = tk.Scrollbar(self.frame1, orient='vertical',
command=self.name.yview)
VScroll1.place(relx=0.971, rely=0.028,
relwidth=0.024, relheight=0.958)
self.name.configure(yscrollcommand=VScroll1.set)
pass
def set_menu(self):# 菜单栏
menu_bar = tk.Menu(self.frame1)
menu_1 = tk.Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(label='文件', menu=menu_1)
menu_1.add_command(label='新建', accelerator='Ctrl+N')
menu_1.add_command(label='打开', accelerator='Ctrl+O',
command=self.open_file)
menu_1.add_command(label='保存', accelerator='Ctrl+S',
command=self.save_file)
menu_1.add_command(label='另存为', accelerator='Shift+Ctrl+S')
menu_1.add_separator()
menu_1.add_command(label='退出', accelerator='Alt+F4')
menu_2 = tk.Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(label='编辑', menu=menu_2)
menu_2.add_command(label='撤销', accelerator='Ctrl+Z',
command=self.set_move)
menu_2.add_command(label='恢复', accelerator='Ctrl+Y')
menu_2.add_separator()
menu_2.add_command(label='剪切', accelerator='Ctrl+X')
menu_2.add_command(label='复制', accelerator='Ctrl+C')
menu_2.add_command(label='粘贴', accelerator='Ctrl+V')
menu_2.add_separator()
menu_2.add_command(label='查找', accelerator='Ctrl+F')
menu_2.add_separator()
menu_2.add_command(label='全选', accelerator='Ctrl+A')
self["menu"] = menu_bar
def set_youjian(self):# 右键菜单
global menu_3
menu_3 = tk.Menu(self, tearoff=0)
menu_3.add_command(label='新建',)
def popupmenu(self):
menu_3.post(self.x_root, self.y_root)
if __name__ == '__main__':
MyApp().mainloop() 用代码标签报一下,看着很不舒服 from asyncio import events
import tkinter as tk
from tkinter import INSERT, LEFT, filedialog
from tkinter.filedialog import *
from tkinter.ttk import Treeview
from PIL import Image
class MyApp(tk.Tk):
def __init__(self):
super().__init__()
self.set_window(800, 500)
self.frame1 = tk.Frame().pack()
self.set_treeview()
self.set_text()
self.set_menu()
self.menu_3 = tk.Menu(self, tearoff=0)
self.menu_3.add_command(label='新建', )
def set_window(win, width, height):
screenwidth = win.winfo_screenwidth()
screenheight = win.winfo_screenheight()
size = '%dx%d+%d+%d' % (width, height,
(screenwidth - width)/2, (screenheight - height)/2)
win.geometry(size)
def set_treeview(self):# 列表目录
tree = Treeview(self.frame1)
tree_1 = tree.insert("", 0, "0", text="目录", values=("0"))
tree_2 = tree.insert(tree_1, 0, "1", text="第一个", values=("1"))
tree_3 = tree.insert(tree_1, 0, "2", text="第二个", values=("2"))
tree.pack(side=LEFT, fill='y')
tree.bind("<Button-3>", self.popupmenu)
def set_text(self):# 文本编辑
global content_text
content_text = tk.Text(self.frame1, width=80, height=10, undo=True)
content_text.pack(side='left', fill='y', padx=10)
self.set_scrollbar(content_text)
def open_file(self, ):# 选择打开什么文件,返回文件名
filename = filedialog.askopenfilename()
f = open(filename, 'r')
f2 = f.read()
content_text.insert(INSERT, f2)
f.close()
def set_move(self):# 撤销
content_text.edit_undo()
pass
def save_file(self):# 保存content_text
filename = filedialog.asksaveasfilename()
with open(filename, 'w') as f:
f.write(content_text.get('1.0', tk.END))
def set_scrollbar(self, name):# 滚动条
self.name = name
VScroll1 = tk.Scrollbar(self.frame1, orient='vertical',
command=self.name.yview)
VScroll1.place(relx=0.971, rely=0.028,
relwidth=0.024, relheight=0.958)
self.name.configure(yscrollcommand=VScroll1.set)
pass
def set_menu(self):# 菜单栏
menu_bar = tk.Menu(self.frame1)
menu_1 = tk.Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(label='文件', menu=menu_1)
menu_1.add_command(label='新建', accelerator='Ctrl+N')
menu_1.add_command(label='打开', accelerator='Ctrl+O',
command=self.open_file)
menu_1.add_command(label='保存', accelerator='Ctrl+S',
command=self.save_file)
menu_1.add_command(label='另存为', accelerator='Shift+Ctrl+S')
menu_1.add_separator()
menu_1.add_command(label='退出', accelerator='Alt+F4')
menu_2 = tk.Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(label='编辑', menu=menu_2)
menu_2.add_command(label='撤销', accelerator='Ctrl+Z',
command=self.set_move)
menu_2.add_command(label='恢复', accelerator='Ctrl+Y')
menu_2.add_separator()
menu_2.add_command(label='剪切', accelerator='Ctrl+X')
menu_2.add_command(label='复制', accelerator='Ctrl+C')
menu_2.add_command(label='粘贴', accelerator='Ctrl+V')
menu_2.add_separator()
menu_2.add_command(label='查找', accelerator='Ctrl+F')
menu_2.add_separator()
menu_2.add_command(label='全选', accelerator='Ctrl+A')
self["menu"] = menu_bar
def popupmenu(self,e):
self.menu_3.post(e.x_root, e.y_root)
if __name__ == '__main__':
MyApp().mainloop() Prozacs 发表于 2022-1-19 17:20
from asyncio import events
import tkinter as tk
from tkinter import INSERT, LEFT, filedialog
成了,谢谢你
页:
[1]