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

"""测试Canvas组件的基本用法,使用面向对象的方式"""


from tkinter import *
from tkinter import messagebox
import random

class Application(Frame):

    def __init__(self,master):
        super().__init__(master)
        self.master=master
        self.pack()
        self.createWidget()

    def createWidget(self):
        self.canvas=Canvas(self,width=300,height=200,bg='green')
        self.canvas.pack()
        # 画一条直线
        line=self.canvas.create_line(10,10,20,20,35,34)
        # 画一个矩形
        rect=self.canvas.create_rectangle(50,50,100,100)
        # 画一个圆
        oval=self.canvas.create_oval(50,50,100,100)

        global photo
        photo=PhotoImage(file="d:/百战程序员/GUI图形用户界面编程/登录.gif")
        self.canvas.create_image(150,170,image=photo)

        Button(self,text="画10个矩形",command=self.draw50Recg).pack(side="left")

    def draw50Recg(self):
        for i in range(0,10):
            x1=random.randrange(int(self.canvas["width"])/2)
            y1=random.randrange(int(self.canvas["height"])/2)
            x2=x1+random.randrange(int(self.canvas["width"])/2)
            y2=y1+random.randrange(int(self.canvas["height"])/2)
            self.canvas.create_rectangle(x1,x2,y1,y2)

if __name__=="__main__":
    root=Tk()
    root.geometry("500x500+300+200")
    app=Application(master=root)
    root.mainloop()


批注 2019-09-24 210043.png

 这三行没有正常运行,但是和教程上的一致。

Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 1563楼
Python 全系列/第二阶段:Python 深入与提高/文件处理 1564楼
Python 全系列/第二阶段:Python 深入与提高/游戏开发-坦克大战 1567楼

image.png

a.txt

老师,我在打开这个文件时,如果文本中出现了中文,就会报错,这个该如何解决呢?

"""开发记事本软件的菜单"""


from tkinter.filedialog import *
from tkinter.colorchooser import *
from tkinter.filedialog import *

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.test)
        menuFile.add_separator()   #添加分割线
        menuFile.add_command(label="退出",accelerator="ctrl+q",command=self.test)

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

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

    def openfile(self):
        with askopenfile(title="打开文本文件") as f:
            print(f.read())


    def test(self):
        pass


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


Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 1568楼
Python 全系列/第二阶段:Python 深入与提高/文件处理 1569楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 1570楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 1571楼
Python 全系列/第二阶段:Python 深入与提高/文件处理 1573楼
Python 全系列/第二阶段:Python 深入与提高/游戏开发-坦克大战 1574楼
Python 全系列/第二阶段:Python 深入与提高/文件处理 1575楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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