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

"""测试canvas组件的基本用法,使用面向对象的方式"""
from tkinter import *
import random


class Application(Frame):

    def __init__(self, master=None):
        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, 30, 20, 40, 50)
        # 画一个矩形
        rect = self.canvas.create_rectangle(50, 50, 100, 100)
        # 画一个椭圆,坐标为椭圆的边界矩形左上角和底部右下角
        oval = self.canvas.create_oval(50, 50, 100, 100)

        global photo
        photo = PhotoImage(file="imgs/logo.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, y1, x2, y2)




if __name__ == '__main__':
    root = Tk()
    root.geometry("400x240+200+300")
    app = Application(master=root)

    root.mainloop()

老师,我的这个代码为啥会报错

Python全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 886楼
Python全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 887楼
Python全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 888楼
Python全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 890楼
Python全系列/第二阶段:Python 深入与提高/文件处理 891楼
Python全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 893楼
Python全系列/第二阶段:Python 深入与提高/游戏开发-坦克大战 894楼
Python全系列/第二阶段:Python 深入与提高/文件处理 895楼
Python全系列/第二阶段:Python 深入与提高/文件处理 896楼

image.png

为什么我程序里的这三个变量没变虚?

"""测试canvas组件的基本用法,使用面向对象的方式"""
from tkinter import *
from tkinter import messagebox
import random

class Application(Frame):

    def __init__(self,master=None):
        super().__init__(master)        # super()代表的是父类的定义,而不是父类对象
        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,30,20,40,50)
        #画一个矩形
        rect=self.canvas.create_rectangle(50,50,100,100)
        #画一个椭圆,坐标两双。为椭圆的边界矩形左上角和底部右下角
        oval=self.canvas.create_oval(50,50,100,100)

        global photo
        photo = PhotoImage(file="image/logo.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,y1,x2,y2)

if __name__ == '__main__':
    root = Tk()
    root.geometry("400x300+200+300")
    app = Application(master=root)
    root.mainloop()


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

课程分类

百战程序员微信公众号

百战程序员微信小程序

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