测试1:画布测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import tkinter as tk def drawCircle(self, x, y, r, **kwargs): return self.create_oval(x-r, y-r, x+r, y+r, **kwargs) top = tk.Tk() top.title("Canvas Test") cvs = tk.Canvas(top, width = 600, height = 400) cvs.pack() cvs.create_line(50, 50, 50, 300) cvs.create_line(100, 50, 200, 300, fill = "red", dash = (4, 4), arrow = tk.LAST) cvs.create_rectangle(200, 50, 400, 200, fill = "blue") cvs.create_oval(450, 50, 550, 200, fill = "green" ) drawCircle(cvs, 450, 300, 50, fill = "red") cvs.create_polygon(200, 250, 350, 250, 350, 350, 220, 300, fill="yellow") top.mainloop() |
测试2:led灯控软件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
from tkinter import * from gpiozero import LED led = LED(17) #定义Button的回调函数 def helloButton(): led.toggle()#led状态翻转 drawCircle(cvs, 150, 100, 50, fill = "blue" if led.value else "red" ) def drawCircle(self, x, y, r, **kwargs): return self.create_oval(x-r, y-r, x+r, y+r, **kwargs) top = Tk() top.title("LED TOGGLE") cvs = Canvas(top, width = 300, height = 200) cvs.pack() drawCircle(cvs, 150, 100, 50, fill = "blue" if led.value else "red") #通过command属性来指定Button的回调函数 Button(top,text = 'LED Button',command = helloButton).pack() top.mainloop()#TK的主程序循环 |
测试3:数码管图形布局
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
from tkinter import * #定义Button的回调函数 def grab(event): #self._y = event.y #self._x = event.x cv.itemconfig(bd1,fill= cv.itemcget(bd1,'fill') == 'red' and 'blue' or 'red') print(event.y,event.x) root = Tk() # 创建一个Canvas,设置其背景色为白色 cv = Canvas(root, #width = 300, #height = 300, #bg='blue' ) # 创建一个矩形,坐标为(10,10,110,110) bd1 = cv.create_rectangle(10+125, 10, 110+125, 20, outline = 'white', fill='red' ) bd2=cv.create_rectangle(10+125, 10+100, 110+125, 20+100, outline = 'white', fill='red' ) bd3=cv.create_rectangle(10+125, 10+200, 110+125, 20+200, outline = 'white', fill='red' ) #cv.bind('',self.grab) cv.bind('',grab) cv.pack() root.mainloop() # 为明显起见,将背景色设置为白色,用以区别root |
测试4:数码管item按键变色
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
from tkinter import * #定义Button的回调函数 def grab(event): canvas = event.widget x = canvas.canvasx(event.x) y = canvas.canvasy(event.y) item = canvas.find_closest(x, y) cv.itemconfig(item,fill= cv.itemcget(item,'fill') == 'red' and 'blue' or 'red') def drawCircle(self, x, y, r, **kwargs): return self.create_oval(x-r, y-r, x+r, y+r, **kwargs) root = Tk() # 创建一个Canvas,设置其背景色为白色 cv = Canvas(root, #width = 300, #height = 300, #bg='blue' ) # 创建一个矩形,坐标为(10,10,110,110) cv.create_rectangle(10+125, 10, 110+125, 20, outline = 'white', fill='red' ) cv.create_rectangle(10+125, 10+100, 110+125, 20+100, outline = 'white', fill='red' ) cv.create_rectangle(10+125, 10+200, 110+125, 20+200, outline = 'white', fill='red' ) drawCircle(cv,110+125+20,20+200-5, 5, outline = 'white', fill = "red") #cv.bind('',self.grab) cv.bind('',grab) cv.pack() root.mainloop() # 为明显起见,将背景色设置为白色,用以区别root |
测试5:完成的画布按键
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
from tkinter import * import tkinter as tk from gpiozero import LEDBoard leds = LEDBoard(2, 3, 4, 14, 17,27,22,23,active_high=False) def drawCircle(self,x,y,r,**kwargs): return self.create_oval(x-r,y-r,x+r,y+r,**kwargs) def grab(event): canvas = event.widget item = canvas.find_closest(event.x, event.y) cv.itemconfig(item,fill= cv.itemcget(item,'fill') == 'pink' and 'blue' or 'pink') #leds[0].on() #print (item[0] , c1) t.delete('1.0', END) t.insert(END, '(') for n in range (0,8) : leds[n].on() if cv.itemcget(n+1,'fill') == 'pink' else leds[n].off() t.insert(END, '1' if cv.itemcget(n+1,'fill') == 'pink' else '0') t.insert(END, ',') t.delete('1.16', END) t.insert(END, '),') root = Tk() leds.on() cv = Canvas(root, width = 600, height = 570, bg = 'white') c1 = cv.create_rectangle(150,10,450,100-40, outline ='white', fill ='pink' ) c7 = cv.create_rectangle(400,100-40,450,300-40, outline ='white', fill ='pink' ) c6 = cv.create_rectangle(400,350-40,450,550-40, outline ='white', fill ='pink' ) c3 = cv.create_rectangle(150,600-40,450,550-40, outline ='white', fill ='pink' ) c5 = cv.create_rectangle(150,350-40,200,550-40, outline ='white', fill ='pink' ) c4 = cv.create_rectangle(150,100-40,200,300-40, outline ='white', fill ='pink' ) c2 = cv.create_rectangle(150,350-40,450,300-40, outline ='white', fill ='pink' ) c8 = drawCircle(cv,510,570-40,30, outline = 'white', fill = 'pink' ) cv.bind('<ButtonRelease-1>',grab) cv.pack() t = Text(root, height = 1)#设置高度为1行高度 t.pack() root.mainloop() |
测试6:gui数码管与LED关联
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
from tkinter import * import tkinter as tk from gpiozero import LEDBoard leds = LEDBoard(2, 3, 4, 14, 17,27,22,23,active_high=False) def drawCircle(self,x,y,r,**kwargs): return self.create_oval(x-r,y-r,x+r,y+r,**kwargs) def grab(event): canvas = event.widget item = canvas.find_closest(event.x, event.y) cv.itemconfig(item,fill= cv.itemcget(item,'fill') == 'pink' and 'blue' or 'pink') #leds[0].on() #print (item[0] , c1) for n in range (0,8) : leds[n].on() if cv.itemcget(n+1,'fill') == 'pink' else leds[n].off() root = Tk() leds.on() cv = Canvas(root, width = 600, height = 800, bg = 'white') c1 = cv.create_rectangle(150,50,450,100, outline ='white', fill ='pink' ) c7 = cv.create_rectangle(400,100,450,300, outline ='white', fill ='pink' ) c6 = cv.create_rectangle(400,350,450,550, outline ='white', fill ='pink' ) c3 = cv.create_rectangle(150,600,450,550, outline ='white', fill ='pink' ) c5 = cv.create_rectangle(150,350,200,550, outline ='white', fill ='pink' ) c4 = cv.create_rectangle(150,100,200,300, outline ='white', fill ='pink' ) c2 = cv.create_rectangle(150,350,450,300, outline ='white', fill ='pink' ) c8 = drawCircle(cv,510,570,30, outline = 'white', fill = 'pink' ) cv.bind('<ButtonRelease-1>',grab) cv.pack() root.mainloop() |
tk电子琴
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
import tkinter as tk import gpiozero m=( 262,286,274,311,349,298,392,440,555,494, 523,587,659,698,784,880,987, 1046,1174,1318,1396,1567,1760,1975 ) bazz=gpiozero.PWMLED(17) bazz.value=0.5 root=tk.Tk() cv=tk.Canvas(root,width=1400,height=450,bg='#908070') def call_press(event): canvas=event.widget item=canvas.find_closest(event.x,event.y) print(item[0]) bazz.frequency =m[item[0]-1] cv.itemconfig(item,fill='#708090') def call_release(event): canvas=event.widget item=canvas.find_closest(event.x,event.y) if item[0]%3: cv.itemconfig(item,fill='white') else: cv.itemconfig(item,fill='black') bazz.off() for i in range(0,1100,100): cv.create_rectangle(i+100+50,10,i+50+50,500,outline ='black',fill = "white") cv.create_rectangle(i+100+50*2,10,i+50+50*2,500,outline ='black',fill = "white") cv.create_rectangle(i+100+50*2-25,10,i+50+50*2-25,200,outline ='white',fill = "Black") cv.bind('<Button-1>',call_press) cv.bind('<ButtonRelease-1>',call_release) cv.pack() root.mainloop() |
转载请注明:徐自远的乱七八糟小站 » 20170912(第6课)[数码管gui测试,电子琴键]树莓派PYPI- tk_画布canvas