canvas.pack,canvaspack参数
python新手提问 谢谢大神了
这个报错的原因是你的缩进少了,导致 draw() 方法不在 Ball类 里面, 办法是从17到27行全部再往后缩进一个单位。不幸的是,我刚调试了一下,你这段代码不光是有这一个问题。靠你自己慢慢找了。
python canvas 拖拽和移动\事件
"""
draw elastic shapes on a canvas on drag, move on right click;
see canvasDraw_tags*.py for extensions with tags and animation
"""
from tkinter import *
trace = False
class CanvasEventsDemo:
def init (self, parent=None):
canvas = Canvas(width=300, height=300, bg='beige')
canvas.pack()
canvas.bind('ButtonPress-1', self.onStart) # click
canvas.bind('B1-Motion', self.onGrow) # and drag
canvas.bind('Double-1', self.onClear) # delete all
canvas.bind('ButtonPress-3', self.onMove) # move latest
self.canvas = canvas
self.drawn = None
self.kinds = [canvas.create_oval, canvas.create_rectangle]
if name == ' main ':
CanvasEventsDemo()
mainloop()
from tkinter import *
def onCanvasClick(event):
print('Got canvas click', event.x, event.y, event.widget)
def onObjectClick(event):
print('Got object click', event.x, event.y, event.widget, end=' ')
print(event.widget.find_closest(event.x, event.y)) # find text object's ID
root = Tk()
canv = Canvas(root, width=100, height=100)
obj1 = canv.create_text(50, 30, text='Click me one')
obj2 = canv.create_text(50, 70, text='Click me two')
canv.bind('Double-1', onCanvasClick) # bind to whole canvas
canv.tag_bind(obj1, 'Double-1', onObjectClick) # bind to drawn item
canv.tag_bind(obj2, 'Double-1', onObjectClick) # a tag works here too
canv.pack()
root.mainloop()
python的tkinter库,求详细用法
tkinter是Python中最有用的库之一,以下有它的详细用法:
准备(必做):
import tkinter
Windows=tkinter.Tk()
canvas=tkinter.Canvas(Windows,width=数字,height=数字)
canvas.pack()
读取图片:变量名=tkinter.PhotoImage(file='....')
放出图片:变量名1=canvas.create_image(数字,数字(两个数字表示坐标),image=变量名,anchor="nw或ne或sw或se或不写,表示锚点")
Windows.title("欢迎") 设置窗口名
canvas.update()更新画面
canvas.delete(变量名1)删除图片
坐标=canvas.coords(变量名1)获取坐标
canvas.itemconfig(改什么,改成什么) 改图片
canvas.move(移什么,x坐标移多少,y坐标移多少)
10.id=canvas.create_text(x坐标,y坐标,text=写什么,fill='填充',font=('字体',字号))
这十个够用一段时间了
希望我的回答能够帮助到您!如果赞同我的观点请您采纳哦!
python 怎么写双人弹球游戏
用python? tkinter模块做一个击打反弹球和球拍的游戏。球在屏幕上飞,玩家需要把它击打回去,只要球落到屏幕底部,游戏就结束。
首先我们做一个在屏幕上到处移动的小球:
#coding:utf-8
from?tkinter?import?*
import?random
import?time
class?Ball:
????#创建一个球类
????def?__init__(self,?canvas,?color):
????????self.canvas?=?canvas
????????self.id?=?canvas.create_oval(10,?10,?25,?25,?fill=color)
????????#返回刚好划小球的id,create_oval创建一个椭圆
????????self.canvas.move(self.id,?245,?100)
????????#把椭圆移动到画布
????????starts?=?[-3,?-2,?-1,?1,?2,?3]
????????random.shuffle(starts)
????????#随机排列
????????self.x?=?starts[0]
????????self.y?=?-3
????????self.canvas_height?=?self.canvas.winfo_height()
????????#获取画布当前高度
????????self.canvas_width?=?self.canvas.winfo_width()
????????#获取画布当前宽度
????def?draw(self):
????????self.canvas.move(self.id,?self.x,?self.y)
????????#让小球水平和垂直移动
????????pos?=?self.canvas.coords(self.id)
????????#coords返回画布上画好的x和y坐标
????????#判断小球是否撞到画布顶部或者底部,保证小球反弹回去,不消失
????????if?pos[1]?=?0:
????????????self.y?=?3
????????if?pos[3]?=?self.canvas_height:
????????????self.y?=?-3
????????if?pos[0]?=?0:
????????????self.x?=?3
????????if?pos[2]?=?self.canvas_width:
????????????self.x?=?-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)
#bd和highlighttthickness是为了保证画布没有边框
canvas.pack()
tk.update()
#动画初始化
ball?=?Ball(canvas,?'red')
while?1:
????#画布一出现会马上消失,为了防止画布消失,用tkinter一直重画
????ball.draw()
????tk.update_idletasks()
????tk.update()
????time.sleep(0.01)
一个会动的小球就做好啦
效果图如下,只是它会动( ̄3 ̄) !
python canvas画布
"demo all basic canvas interfaces"
from tkinter import *
canvas = Canvas(width=525, height=300, bg='white') # 0,0 is top left corner
canvas.pack(expand=YES, fill=BOTH) # increases down, right
canvas.create_line(100, 100, 200, 200) # fromX, fromY, toX, toY
canvas.create_line(100, 200, 200, 300) # draw shapes
for i in range(1, 20, 2):
canvas.create_line(0, i, 50, i)
canvas.create_oval(10, 10, 200, 200, width=2, fill='blue')
canvas.create_arc(200, 200, 300, 100)
canvas.create_rectangle(200, 200, 300, 300, width=5, fill='red')
canvas.create_line(0, 300, 150, 150, width=10, fill='green')
photo=PhotoImage(file='../gifs/ora-lp4e.gif')
canvas.create_image(325, 25, image=photo, anchor=NW) # embed a photo
widget = Label(canvas, text='Spam', fg='white', bg='black')
widget.pack()
canvas.create_window(100, 100, window=widget) # embed a widget
canvas.create_text(100, 280, text='Ham') # draw some text
mainloop()