会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132464个问题
Python 全系列/第十阶段:Flask百战电商后台项目/Flask百战电商后台项目 12976楼
JAVA 全系列/第四阶段:网页编程和设计/JavaScript语言 12977楼
WEB前端全系列/第二阶段:JavaScript编程模块/字符串与数组 12980楼
Python 全系列/第二阶段:Python 深入与提高/游戏开发-坦克大战 12981楼

# coding=utf-8
# 记事本项目

from tkinter import *
from tkinter.filedialog import *

class Application(Frame):
    def __init__(self,master=None):
        super().__init__(master)
        self.master = master
        self.pack()
        self.textpad = None
        self.creatwidget()

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

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

        # 将子菜单加入到主菜单
        menubar.add_cascade(label='文件(F)', menu=menuFile)
        menubar.add_cascade(label='编辑(E)', menu=menuEdit)
        menubar.add_cascade(label='帮助(H)', menu=menuHelp)

        # 添加菜单项
        menuFile.add_command(label='新建',accelerator='ctrl+n',command=self.test)
        menuFile.add_command(label='打开', accelerator='ctrl+o', command=self.openfile)
        menuFile.add_command(label='保存', accelerator='ctrl+s', command=self.test)
        menuFile.add_separator() # 添加分割线
        menuFile.add_command(label='退出', accelerator='ctrl+q', command=self.test)

        #将主菜单加到root
        root['menu']=menubar

        #文本编辑区
        self.textpad = Text(root,width=50,height=30).pack()

    def test(self):
        pass

    def openfile(self):
        with askopenfile(title='打开文本文件') as f:
            self.textpad.insert(INSERT, f.read())
            self.filename = f.name

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

d83828f45637d32162b3bb759b3647b.png老师我想请问一下这里显示AttributeError: 'NoneType' object has no attribute 'insert' 要怎么解决

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

Microsoft Windows [版本 10.0.18363.1198]
(c) 2019 Microsoft Corporation。保留所有权利。

F:\百战python\代码\GUI图形>pyinstaller -F pen.py
'pyinstaller' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
Microsoft Windows [版本 10.0.18363.1198]
(c) 2019 Microsoft Corporation。保留所有权利。

F:\百战python\代码\GUI图形>pyinstaller -F pen.py
80 INFO: PyInstaller: 4.1
81 INFO: Python: 3.7.8
82 INFO: Platform: Windows-10-10.0.18362-SP0
83 INFO: wrote F:\百战python\代码\GUI图形\pen.spec
85 INFO: UPX is not available.
88 INFO: Extending PYTHONPATH with paths
['F:\\百战python\\代码\\GUI图形', 'F:\\百战python\\代码\\GUI图形']
110 INFO: checking Analysis
110 INFO: Building Analysis because Analysis-00.toc is non existent
110 INFO: Initializing module dependency graph...
113 INFO: Caching module graph hooks...
125 INFO: Analyzing base_library.zip ...
2886 INFO: Processing pre-find module path hook distutils from 'f:\\软件\\python\\lib\\site-packages\\PyInstaller\\hooks\\pre_find_module_path\\hook-distutils.py'.
2887 INFO: distutils: retargeting to non-venv dir 'f:\\软件\\python\\lib'
5509 INFO: Caching module dependency graph...
5608 INFO: running Analysis Analysis-00.toc
5612 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  required by f:\软件\python\python.exe
5685 INFO: Analyzing F:\百战python\代码\GUI图形\pen.py
5896 INFO: Processing module hooks...
5896 INFO: Loading module hook 'hook-difflib.py' from 'f:\\软件\\python\\lib\\site-packages\\PyInstaller\\hooks'...
5897 INFO: Excluding import of doctest from module difflib
5898 INFO: Loading module hook 'hook-distutils.py' from 'f:\\软件\\python\\lib\\site-packages\\PyInstaller\\hooks'...
5899 INFO: Loading module hook 'hook-encodings.py' from 'f:\\软件\\python\\lib\\site-packages\\PyInstaller\\hooks'...
5984 INFO: Loading module hook 'hook-heapq.py' from 'f:\\软件\\python\\lib\\site-packages\\PyInstaller\\hooks'...
5986 INFO: Excluding import of doctest from module heapq
5986 INFO: Loading module hook 'hook-pickle.py' from 'f:\\软件\\python\\lib\\site-packages\\PyInstaller\\hooks'...
5987 INFO: Excluding import of argparse from module pickle
5987 INFO: Loading module hook 'hook-sysconfig.py' from 'f:\\软件\\python\\lib\\site-packages\\PyInstaller\\hooks'...
5988 INFO: Loading module hook 'hook-xml.py' from 'f:\\软件\\python\\lib\\site-packages\\PyInstaller\\hooks'...
6324 INFO: Loading module hook 'hook-_tkinter.py' from 'f:\\软件\\python\\lib\\site-packages\\PyInstaller\\hooks'...
6468 INFO: checking Tree
6468 INFO: Building Tree because Tree-00.toc is non existent
6468 INFO: Building Tree Tree-00.toc
6537 INFO: checking Tree
6537 INFO: Building Tree because Tree-01.toc is non existent
6543 INFO: Building Tree Tree-01.toc
6620 INFO: checking Tree
6620 INFO: Building Tree because Tree-02.toc is non existent
6622 INFO: Building Tree Tree-02.toc
6642 INFO: Looking for ctypes DLLs
6642 INFO: Analyzing run-time hooks ...
6646 INFO: Including run-time hook 'f:\\软件\\python\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth__tkinter.py'
6651 INFO: Looking for dynamic libraries
6918 INFO: Looking for eggs
6918 INFO: Using Python library f:\软件\python\python37.dll
6924 INFO: Found binding redirects:
[]
6928 INFO: Warnings written to F:\百战python\代码\GUI图形\build\pen\warn-pen.txt
6964 INFO: Graph cross-reference written to F:\百战python\代码\GUI图形\build\pen\xref-pen.html
7001 INFO: checking PYZ
7001 INFO: Building PYZ because PYZ-00.toc is non existent
7002 INFO: Building PYZ (ZlibArchive) F:\百战python\代码\GUI图形\build\pen\PYZ-00.pyz
7591 INFO: Building PYZ (ZlibArchive) F:\百战python\代码\GUI图形\build\pen\PYZ-00.pyz completed successfully.
7600 INFO: checking PKG
7601 INFO: Building PKG because PKG-00.toc is non existent
7601 INFO: Building PKG (CArchive) PKG-00.pkg
14587 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
14615 INFO: Bootloader f:\软件\python\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe
14616 INFO: checking EXE
14617 INFO: Building EXE because EXE-00.toc is non existent
14618 INFO: Building EXE from EXE-00.toc
14619 INFO: Appending archive to EXE F:\百战python\代码\GUI图形\dist\pen.exe
14631 INFO: Building EXE from EXE-00.toc completed successfully.

image.png


老师,我想问一下,打包成exe文件这个过程,我在pycharm中提示我不是内部命令,但我在cmd里就能打包是怎么回事?

Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 12984楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 12985楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO流技术 12987楼

#coding=utf-8
'''组件对象的绑定
1.通过command属性绑定(适合简单不需获取event对象) Button(root,text="登录",command=login)
2.通过bind()方法绑定(适合需要获取evnet对象) c1 = Canvas(); c1.bind("<Button-1>",drawLine)
   组件类的绑定
调用对象的bind_class函数,将该组件类所有的组件绑定事件:
w.bind_class("Widget","event",eventhanler) 比如:btn01.bind_class("Button","<Button-1>",func)
'''
#多种事件绑定方式汇总
from tkinter import *
root = Tk()
root.geometry("270x30")

def mouseTest1(event):
    print("bind()方式绑定,可获取event对象")
    print(event.widget)

def mouseTest2(a, b):
    print("a={0}, b={1}".format(a, b))
    print("command方式绑定,不能直接获取event对象")

def mouseTest3(event):
    print("钟呢呢右键单击事件,绑定给所有按钮啦!!")
    print(event.widget)

b1 = Button(root, text="测试bind()绑定") #第一个按钮b1
b1.pack(side="left")
b1.bind("<Button-1>", mouseTest1) #bind方式绑定事件

b2 = Button(root, text="测试command绑定", command=lambda: mouseTest2("xiannv", "zhongnene"))#第二个按钮b2 通过lambda直接传参
b2.pack(side="left")

b1.bind_class("<Button>", "<Button-2>", mouseTest3)#给所有Button按钮都绑定右键单击事件<Button-2>

root.mainloop()

<button-2>为鼠标中键,然而单击鼠标中键没反应

看视频高老师是单击的鼠标右键 我也试了但也没反应

Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 12989楼
JAVA 全系列/第十一阶段:消息中间件与高并发处理/Nginx 12990楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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