20170913 (第7课)树莓派PYPI- tk文本框-Text()

树莓派Python课程 徐 自远 773℃

Python Tkinter教程之Text(1)篇
Python Tkinter教程之Text(2)篇
Python GUI – Tkinter Text(文本)

测试1:text与按键测试
[cc lang=”python”]
#参考代码:
# -*- coding: cp936 -*-
from tkinter import *
root = Tk()
root.title(“hello world”)
root.geometry()

def printhello():
t.insert(‘1.0’, “hello\n”)

t = Text(root,
width = 50,
height = 6
)
t.pack()
Button(root, text=”press”, command = printhello).pack()
root.mainloop()
[/cc]

测试2:Text中添加删除按键

[cc lang=”python”]
# -*- coding: cp936 -*-
from tkinter import *
root = Tk()
root.title(“hello world”)
root.geometry()

def printhello():
t.insert(‘1.0’, “hello\n”)

def clr():
t.delete(‘1.0’, END)
t = Text(root,
width = 50,
height = 6
)
t.pack()
Button(root, text=”press”, command = printhello).pack()
Button(root, text=”clr”, command = clr).pack()
root.mainloop()
[/cc]

测试3:数码管取模小程序(基于Text)[完成]

[cc lang=”python”]
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)#删除最后一个逗号,第一行第16个字符
#t.delete(END-1, END)#删除最后一个逗号,第一行第16个字符(这句话有问题,慎用)
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(‘‘,grab)

cv.pack()
t = Text(root, height = 1)
t.pack()
root.mainloop()
[/cc]

[cc lang=”python”]

[/cc]

[cc lang=”python”]

[/cc]

[cc lang=”python”]

[/cc]

[cc lang=”python”]

[/cc]

[cc lang=”python”]

[/cc]

[cc lang=”python”]

[/cc]

[cc lang=”python”]

[/cc]

[cc lang=”python”]

[/cc]

[cc lang=”python”]

[/cc]

转载请注明:徐自远的乱七八糟小站 » 20170913 (第7课)树莓派PYPI- tk文本框-Text()

喜欢 (0)

苏ICP备18041234号-1 bei_an 苏公网安备 32021402001397号