会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132370个问题

from tkinter.filedialog import *
from tkinter.colorchooser import *
#encoding='UTF-8'
class Application(Frame):
    def __init__(self,master=None):
        super().__init__(master)
        self.master=master
        self.textpad=None
        self.pack()
        self.createWidget()

    def createWidget(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.savefile)
        menuFile.add_separator()      #  添加分割线
        menuFile.add_command(label='退出', accelerator='Ctrl+q', command=self.exit)

        # 将主菜单栏加到根窗口

        root['menu']=menubar
        # 文本编辑区

        self.textpad=Text(root, width=50, height=30)
        self.textpad.pack()

        # 创建上下菜单

        self.contextMenu=Menu(root)
        self.contextMenu.add_command(label='背景颜色', command=self.test)
        # 为右键绑定事件

        root.bind('<Button-3>', self.createContextMenu)
    def openfile(self):
        #self.textpad.delete(1.0,'end')
        with askopenfile(title='打开文本文件')as f:
            self.textpad.insert(INSERT, f.read())
            self.filename=f.name

    def savefile(self):
        with open(self.filename, 'w')as f :
            c=self.textpad.get(1.0, END)
            f.write(c)
    def exit(self):
        root.quit()
    def test(self):
        pass
    def createContextMenu(self,event):
        self.contextMenu.post(event.x_root, event.y_root)
if __name__=='__main__':
    root=Tk()
    root.geometry('450x300+200+300')
    app=Application(master=root)
    root.title('记事本')
    root.mainloop()

老师:

你好!

就是我在打开文件的时候,如果文件里面是汉字。就会报错:‘'gbk' codec can't decode byte 0x88 in position 14: illegal multibyte sequence’。如果是英文就没事。请问是怎么回事



Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 1982楼
Python 全系列/第二阶段:Python 深入与提高/文件处理 1984楼
Python 全系列/第二阶段:Python 深入与提高/异常机制 1985楼

我电脑上运行视频上的代码时是单击鼠标中键才有反应的

# coding=utf-8
# 测试 多种事件绑定方式汇总
from tkinter import *
root = Tk();root.geometry("270x50")
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.pack(side="left")
#bind方式绑定事件
b1.bind("<Button-1>",mouseTest1)
#command属性直接绑定事件
b2 = Button(root, text="测试 command2",command=lambda: mouseTest2("gaoqi","xixi"))
b2.pack(side="left")
#给所有Button按钮都绑定右键单击事件<Button-2>
b1.bind_class("Button","<Button-2>",mouseTest3)
root.mainloop()

修改成<Button-3>时单击右键才有反应

# coding=utf-8
# 测试 多种事件绑定方式汇总
from tkinter import *
root = Tk();root.geometry("270x50")
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.pack(side="left")
#bind方式绑定事件
b1.bind("<Button-1>",mouseTest1)
#command属性直接绑定事件
b2 = Button(root, text="测试 command2",command=lambda: mouseTest2("gaoqi","xixi"))
b2.pack(side="left")
#给所有Button按钮都绑定右键单击事件<Button-3>
b1.bind_class("Button","<Button-3>",mouseTest3)
root.mainloop()

这是我自己电脑问题还是什么?

Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 1986楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 1987楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 1988楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 1989楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 1990楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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