会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132464个问题
Python 全系列/第一阶段:Python入门/控制语句 31145楼
JAVA 全系列/第六阶段:项目管理与SSM框架/Spring 31146楼
JAVA 全系列/第十五阶段:Spring Session会话管理/Spring Session 31147楼
JAVA 全系列/第六阶段:项目管理与SSM框架/Mybatis 31150楼
Python 全系列/第十七阶段:数据分析-数学知识(旧)/补充数学知识 31151楼
JAVA 全系列/第一阶段:JAVA 快速入门/数组和数据存储 31152楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器(旧) 31153楼
Python 全系列/第一阶段:Python入门/Python入门(动画版) 31154楼

# 导入模块
from tkinter import *
from tkinter.colorchooser import *
from tkinter.filedialog import *




#
class Application(Frame):           # 父系Frame-可以包含其他小部件并具有3D边框的框架小部件

    def __init__(self,master=None):
        super().__init__(master)
        self.master = master
        self.textpad = None
        self.pack()
        self.createWidget()         # 定义主菜单方法
        self.contextMenu = None     # contextMenu 上下文菜单对象

    def createWidget(self):
    # 创建主菜单栏
        menubar = Menu(root)

        #menuedit = Menu(menubar2, tearoff=1)  # 菜单栏    添加菜单编辑器
        #menubar2.add_cascade(label="编辑", menu=menuedit)  # cascade=串联,add_cascade语句添加层次菜单项

        # menuedit添加子菜单项目
        #menuedit.add_command(label="剪切")
        #menuedit.add_command(label="复制")
        #menuedit.add_command(label="粘贴")

    #创建子菜单
        menuFile = Menu(menubar)
        menuEdit = Menu(menubar)
        menueHelp = Menu(menubar)

        menubar.add_cascade(label="文件F",menu=menuFile)
        menubar.add_cascade(label="编辑E", menu=menuEdit)
        menubar.add_cascade(label="帮助H", menu=menueHelp)

        # 添加子菜单项目
        menuFile.add_command(label="新建",accelerator="ctel+n",command=self.test)
        menuFile.add_command(label="打开", accelerator="ctel+o", command=self.test)
        menuFile.add_command(label="保存", accelerator="ctel+s", command=self.test)
        menuFile.add_command(label="退出", accelerator="ctel+q", command=self.test)

       # 将主菜单添加到跟主窗口
        root["menu"]=menubar

        # 定义文字编辑区
        self.textpad = Text(root,width=500,height=100)
        self.textpad.pack()


        root.bind("<Button-3>", self.text)


        self.contextMenu = Menu(root)  # 菜单2=菜单栏(root)
        self.contextMenu.add_command(label="颜色", command=self.openAskColor)  # 菜单添加项目颜色,并关联事件=openAskcolor



    # test(event)窗口实时监测方法;
    def text(self, event):
        self.contextMenu.post(event.x_root, event.y_root)


    # 定义一个方法用来调出背景色
    def openAskColor(self):
        s1 = askcolor(color="red", title="选择背景色")
        self.textpad.config(bg=s1[1])

    def test():
        pass

        menubar2 = Menu(root)  # 菜单2=菜单栏(root)







# 创建菜单






   # w1 = Text(root, width=500, height=300)
   # w1.bind("<Button-3>", test)


if __name__ == '__main__':
    root =Tk()
    root.geometry("450x300+200+300")
    root.title("百战程序员的简易记事本")
    app = Application(master=root)


    root.mainloop()

运行报错,请指导:

C:\Users\Administrator\AppData\Local\Programs\Python\Python38\python.exe "H:/python DM/MYPY_0204_GUI/notebook.py"

Exception in Tkinter callback

Traceback (most recent call last):

  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\tkinter\__init__.py", line 1883, in __call__

    return self.func(*args)

  File "H:/python DM/MYPY_0204_GUI/notebook.py", line 66, in text

    self.contextMenu.post(event.x_root, event.y_root)

AttributeError: 'NoneType' object has no attribute 'post'


Process finished with exit code 0

需要怎么改?怎么就错了?

Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 31155楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637