注册 登录
编程论坛 Python论坛

在新建文件中引入了画图模块,代码正确,但就是不显示窗口,怎么办?

Redeyes 发布于 2017-02-11 17:46, 2888 次点击
软件环境:Python3.6.0
问题描述:在新建文件中添加了turtle模块,对象也建立了,运行也正确,想让它显示出那个窗口,但就是不出来,不知道该怎么办。
          请各位指点一二,谢谢了!
程序代码:
from tkinter import*  
import random  ##引入random模块
import time  ##引入time模块

    ## 创建Ball类 ##
class Ball:
    def _init_(self,canvas,color):
        self.canvas = canvas
        self.id = canvas.create_oval(10,10,25,25,fill=color)  ##五个参数,左上角的x,y坐标,右下角的x,y坐标,椭圆形的填充颜色
        self.canvas.move = canvas.move(self.id,245,100)  ##把椭圆形移到画布中心
        starts = [-3,-2,-1,1,2,3]


    ## 创建游戏的画布 ##
        tk = Tk()
        tk.title("Game")  ##给窗口加上标题
        tk.resizable(0,0)  ##窗口的大小在水平方向和垂直方向上都不能改变
        tk.wm_attributes("-topmost",1)  ##把包含我们画布的窗口放到所有其他窗口之前
        canvas = Canvas(tk,width=500,height=400,bd=0,highlightthickness=0)
        canvas.pack()
        tk.update()

        ball = Ball(canvas,'red') ## 定义小球的颜色 ##

        while 1:
            tk.update_idletasks()
            tk.update()
            time.sleep(0.01)
2 回复
#2
Redeyes2017-02-11 17:49
搞错了,不是turtle,是tkinter模块
#3
Redeyes2017-02-16 00:34
修改后的代码:
程序代码:
class Ball:
    def __init__(self, color):
        tk = Tk()
        tk.title("Game")  ##给窗口加上标题
        tk.resizable(0,0)  ##窗口的大小在水平方向和垂直方向上都不能改变
        tk.wm_attributes("-topmost",1)  ##把包含我们画布的窗口放到所有其他窗口之前
        self.canvas = Canvas(tk,width=500, height=400, bd=0, highlightthickness=0)
        self.id = self.canvas.create_oval(10,10,25,25, fill=color)  ##五个参数,左上角的x,y坐标,右下角的x,y坐标,椭圆形的填充颜色
        self.canvas.move(self.id, 245,100)  ##把椭圆形移到画布中心
        self.canvas.pack()
        tk.update()

        while 1:
            tk.update_idletasks()
            tk.update()
            time.sleep(0.01)  ##休息百分之一秒

ball = Ball('red')
1