
Lv.1
签名:QQ:2246454041
在 【Python作品分享】彩色画板(tk)【作品秀】 中回复
from PIL import Image from PIL import Image, ImageTk, ImageGrab import tkinter.colorchooser import tkinter.filedialog import time import tkinter.simpledialog import tkinter
def center_window(top, w, h): ws = app.winfo_screenwidth() hs = app.winfo_screenheight() app.geometry('%dx%d' % (w, h))
app = tkinter.Tk() app.title('我的画图') center_window(app, 800, 600) yesno = tkinter.IntVar(value=0) what = tkinter.IntVar(value=1) X = tkinter.IntVar(value=0) Y = tkinter.IntVar(value=0) foreColor = '#000000' backColor = '#FFFFFF' image = tkinter.PhotoImage() canvas = tkinter.Canvas(app, bg='white', width=800, height=600) canvas.create_image(800, 600, image=image) lastDraw = 0 end = [0] size = "20"
def getter(widget): time.sleep(0.5) x = app.winfo_x()+widget.winfo_x() y = app.winfo_y()+widget.winfo_y() if app.winfo_x() < 0: x = 0 if app.winfo_y() < 0: y = 0 x1 = x+widget.winfo_width()+200 y1 = y+widget.winfo_height()+200 filename = tkinter.filedialog.asksaveasfilename( filetypes=[('.jpg', 'JPG')], initialdir='C:\\Users\\lin042\\Desktop\\') ImageGrab.grab().crop((x, y, x1, y1)).save(filename)
def onLeftButtonDown(event): yesno.set(1) X.set(event.x) Y.set(event.y) if what.get() == 4: canvas.create_text(event.x, event.y, font=( "微软雅黑",int(size)), text=text, fill=foreColor) what.set(1)
def onLeftButtonMove(event): global lastDraw if yesno.get() == 0: return if what.get() == 1: lastDraw = canvas.create_line( X.get(), Y.get(), event.x, event.y, fill=foreColor) X.set(event.x) Y.set(event.y) elif what.get() == 2: try: canvas.delete(lastDraw) except Exception as e: pass lastDraw = canvas.create_line(X.get(), Y.get(), event.x, event.y, fill=foreColor) elif what.get() == 3: try: canvas.delete(lastDraw) except Exception as e: pass lastDraw = canvas.create_rectangle(X.get(), Y.get(), event.x, event.y, outline=foreColor) elif what.get() == 5: lastDraw = canvas.create_rectangle(event.x-25, event.y-25, event.x+25, event.y+25, outline=backColor) elif what.get() == 6: try: canvas.delete(lastDraw) except Exception as e: pass lastDraw = canvas.create_oval(X.get(), Y.get(), event.x, event.y, fill=backColor, outline=foreColor)
def onLeftButtonUp(event): global lastDraw if what.get() == 2: lastDraw = canvas.create_line( X.get(), Y.get(), event.x, event.y, fill=foreColor) elif what.get() == 3: lastDraw = canvas.create_rectangle( X.get(), Y.get(), event.x, event.y, outline=foreColor) elif what.get() == 6: lastDraw = canvas.create_oval( X.get(), Y.get(), event.x, event.y, outline=foreColor) yesno.set(0) end.append(lastDraw)
def onRightButtonUp(event): menu.post(event.x_root, event.y_root)
canvas.bind('<Button-1>', onLeftButtonDown) canvas.bind('<B1-Motion>', onLeftButtonMove) canvas.bind('<ButtonRelease-1>', onLeftButtonUp) canvas.bind('<ButtonRelease-3>', onRightButtonUp) canvas.pack(fill=tkinter.BOTH, expand=tkinter.YES) menu = tkinter.Menu(app, tearoff=0)
def Open(): filename = tkinter.filedialog.askopenfilename(title='导入图片', filetypes=[('image', '*.jpg *.png *.gif')]) if filename: global image image = Image.open(filename) image = image.resize((800, 600), Image.ANTIALIAS) image = ImageTk.PhotoImage(image) canvas.create_image(400, 300, image=image)
menu.add_command(label='导入', command=Open)
def Clear(): global lastDraw, end for item in canvas.find_all(): canvas.delete(item) end = [0] lastDraw = 0
menu.add_command(label='清屏', command=Clear)
def Back(): global end try: for i inrange(end[-2], end[-1]+1): canvas.delete(i) end.pop() except: end = [0]
menu.add_command(label='撤销', command=Back)
def Save(): getter(canvas)
menu.add_command(label='保存', command=Save) menu.add_separator() menuType = tkinter.Menu(menu, tearoff=0)
def drawCurve(): what.set(1)
menuType.add_command(label='铅笔', command=drawCurve)
def drawLine(): what.set(2)
menuType.add_command(label='直线', command=drawLine)
def drawRectangle(): what.set(3)
menuType.add_command(label='矩形', command=drawRectangle)
def drawCircle(): what.set(6)
menuType.add_command(label='圆形', command=drawCircle)
def drawText(): global text, size text = tkinter.simpledialog.askstring(title='输入文本', prompt='') if text != None: size = tkinter.simpledialog.askinteger( '输入字号', prompt='', initialvalue=20) if size == None: size = "20" what.set(4)
menuType.add_command(label='文本', command=drawText) menuType.add_separator()
def chooseForeColor(): global foreColor foreColor = tkinter.colorchooser.askcolor()[1]
menuType.add_command(label='选择前景色', command=chooseForeColor)
def chooseBackColor(): global backColor backColor = tkinter.colorchooser.askcolor()[1]
menuType.add_command(label='选择背景色', command=chooseBackColor)
def onErase(): what.set(5)
menuType.add_command(label='橡皮擦', command=onErase) menu.add_cascade(label='工具栏', menu=menuType) app.mainloop()
2021-06-13T16:51:56 点赞:0
在 有没有大佬 中回复
import pytesseract
from PIL import Imange
image=Image.open(...)
a = pytesseract.image_to_string(image)
print(a)
这样就可以了
填。。。的地方填图片路径或名称
2021-06-24T10:04:45 点赞:0
在 各位,客户端运行不了vpython 中回复
错了,vpython的导入错了, 是fromvpythonimport× 不然你vector()是python自带的?????
2021-06-27T11:11:40 点赞:0
在 各位,客户端运行不了vpython 中回复
from vpython import *
import pygame
sphere_location = vector(0,0,0)
box._location = vector(2,0,0)
sphere(pos=sphere_location)2021-06-27T11:29:31 点赞:0
在 如何在海龟编辑器上导入图片并显示 中回复
from PIL import Image
im = Image.open('............')
im.show()
PIL库
2021-06-29T11:39:32 点赞:0
在 计算器V1.6/V2.0 中回复
import math
import tkinter
root = tkinter.Tk()
root.resizable(width=False, height=False)
# 是否按下了运算符
IS_CALC = False
# 存储数字
STORAGE = []
# 显示框最多显示多少个字符
MAXSHOWLEN = 18
# 当前显示的数字
CurrentShow = tkinter.StringVar()
CurrentShow.set('0')
'''按下数字键(0-9)'''
def pressNumber(number):
global IS_CALC
if IS_CALC:
CurrentShow.set('0')
IS_CALC = False
if CurrentShow.get() == '0':
CurrentShow.set(number)
else:
if len(CurrentShow.get()) < MAXSHOWLEN:
CurrentShow.set(CurrentShow.get() + number)
'''按下小数点'''
def pressDP():
global IS_CALC
if IS_CALC:
CurrentShow.set('0')
IS_CALC = False
if len(CurrentShow.get().split('.')) == 1:
if len(CurrentShow.get()) < MAXSHOWLEN:
CurrentShow.set(CurrentShow.get() + '.')
'''清零'''
def clearAll():
global STORAGE
global IS_CALC
STORAGE.clear()
IS_CALC = False
CurrentShow.set('0')
'''清除当前显示框内所有数字'''
def clearCurrent():
CurrentShow.set('0')
'''删除显示框内最后一个数字'''
def delOne():
global IS_CALC
if IS_CALC:
CurrentShow.set('0')
IS_CALC = False
if CurrentShow.get() != '0':
if len(CurrentShow.get()) > 1:
CurrentShow.set(CurrentShow.get()[:-1])
else:
CurrentShow.set('0')
'''计算答案修正'''
def modifyResult(result):
result = str(result)
if len(result) > MAXSHOWLEN:
if len(result.split('.')[0]) > MAXSHOWLEN:
result = 'Overflow'
else:
# 直接舍去不考虑四舍五入问题
result = result[:MAXSHOWLEN]
return result
'''按下运算符'''
def pressOperator(operator):
global STORAGE
global IS_CALC
if operator == '+/-':
if CurrentShow.get().startswith('-'):
CurrentShow.set(CurrentShow.get()[1:])
else:
CurrentShow.set('-'+CurrentShow.get())
elif operator == '1/x':
try:
result = 1 / float(CurrentShow.get())
except:
result = 'illegal operation'
result = modifyResult(result)
CurrentShow.set(result)
IS_CALC = True
elif operator == 'sqrt':
try:
result = math.sqrt(float(CurrentShow.get()))
except:
result = 'illegal operation'
result = modifyResult(result)
CurrentShow.set(result)
IS_CALC = True
elif operator == 'MC':
STORAGE.clear()
elif operator == 'MR':
if IS_CALC:
CurrentShow.set('0')
STORAGE.append(CurrentShow.get())
expression = ''.join(STORAGE)
try:
result = eval(expression)
except:
result = 'illegal operation'
result = modifyResult(result)
CurrentShow.set(result)
IS_CALC = True
elif operator == 'MS':
STORAGE.clear()
STORAGE.append(CurrentShow.get())
elif operator == 'M+':
STORAGE.append(CurrentShow.get())
elif operator == 'M-':
if CurrentShow.get().startswith('-'):
STORAGE.append(CurrentShow.get())
else:
STORAGE.append('-' + CurrentShow.get())
elif operator in ['+', '-', '*', '/', '%']:
STORAGE.append(CurrentShow.get())
STORAGE.append(operator)
IS_CALC = True
elif operator == '=':
if IS_CALC:
CurrentShow.set('0')
STORAGE.append(CurrentShow.get())
expression = ''.join(STORAGE)
try:
result = eval(expression)
# 除以0的情况
except:
result = 'illegal operation'
result = modifyResult(result)
CurrentShow.set(result)
STORAGE.clear()
IS_CALC = True
def Demo():
root.minsize(320, 420)
root.title('计算器')
label = tkinter.Label(root, textvariable=CurrentShow,
bg='black', anchor='e', bd=5, fg='white', font=('楷体', 20))
label.place(x=20, y=50, width=280, height=50)
button1_1 = tkinter.Button(
text='MC', bg='#666', bd=2, command=lambda: pressOperator('MC'))
button1_1.place(x=20, y=110, width=50, height=35)
# ----Memory read
button1_2 = tkinter.Button(
text='MR', bg='#666', bd=2, command=lambda: pressOperator('MR'))
button1_2.place(x=77.5, y=110, width=50, height=35)
# ----Memory save
button1_3 = tkinter.Button(
text='MS', bg='#666', bd=2, command=lambda: pressOperator('MS'))
button1_3.place(x=135, y=110, width=50, height=35)
# ----Memory +
button1_4 = tkinter.Button(
text='M+', bg='#666', bd=2, command=lambda: pressOperator('M+'))
button1_4.place(x=192.5, y=110, width=50, height=35)
# ----Memory -
button1_5 = tkinter.Button(
text='M-', bg='#666', bd=2, command=lambda: pressOperator('M-'))
button1_5.place(x=250, y=110, width=50, height=35)
button2_1 = tkinter.Button(
text='del', bg='#666', bd=2, command=lambda: delOne())
button2_1.place(x=20, y=155, width=50, height=35)
# ----清除当前显示框内所有数字
button2_2 = tkinter.Button(
text='CE', bg='#666', bd=2, command=lambda: clearCurrent())
button2_2.place(x=77.5, y=155, width=50, height=35)
# ----清零(相当于重启)
button2_3 = tkinter.Button(
text='C', bg='#666', bd=2, command=lambda: clearAll())
button2_3.place(x=135, y=155, width=50, height=35)
# ----取反
button2_4 = tkinter.Button(
text='+/-', bg='#666', bd=2, command=lambda: pressOperator('+/-'))
button2_4.place(x=192.5, y=155, width=50, height=35)
# ----开根号
button2_5 = tkinter.Button(text='sqrt', bg='#666',
bd=2, command=lambda: pressOperator('sqrt'))
button2_5.place(x=250, y=155, width=50, height=35)
# --第三行
# ----7
button3_1 = tkinter.Button(text='7', bg='#bbbbbb',
bd=2, command=lambda: pressNumber('7'))
button3_1.place(x=20, y=200, width=50, height=35)
# ----8
button3_2 = tkinter.Button(text='8', bg='#bbbbbb',
bd=2, command=lambda: pressNumber('8'))
button3_2.place(x=77.5, y=200, width=50, height=35)
# ----9
button3_3 = tkinter.Button(text='9', bg='#bbbbbb',
bd=2, command=lambda: pressNumber('9'))
button3_3.place(x=135, y=200, width=50, height=35)
# ----除
button3_4 = tkinter.Button(text='/', bg='#708069',
bd=2, command=lambda: pressOperator('/'))
button3_4.place(x=192.5, y=200, width=50, height=35)
# ----取余
button3_5 = tkinter.Button(text='%', bg='#708069',
bd=2, command=lambda: pressOperator('%'))
button3_5.place(x=250, y=200, width=50, height=35)
# --第四行
# ----4
button4_1 = tkinter.Button(text='4', bg='#bbbbbb',
bd=2, command=lambda: pressNumber('4'))
button4_1.place(x=20, y=245, width=50, height=35)
# ----5
button4_2 = tkinter.Button(text='5', bg='#bbbbbb',
bd=2, command=lambda: pressNumber('5'))
button4_2.place(x=77.5, y=245, width=50, height=35)
# ----6
button4_3 = tkinter.Button(text='6', bg='#bbbbbb',
bd=2, command=lambda: pressNumber('6'))
button4_3.place(x=135, y=245, width=50, height=35)
# ----乘
button4_4 = tkinter.Button(text='*', bg='#708069',
bd=2, command=lambda: pressOperator('*'))
button4_4.place(x=192.5, y=245, width=50, height=35)
# ----取导数
button4_5 = tkinter.Button(text='1/x', bg='#708069',
bd=2, command=lambda: pressOperator('1/x'))
button4_5.place(x=250, y=245, width=50, height=35)
# --第五行
# ----3
button5_1 = tkinter.Button(text='3', bg='#bbbbbb',
bd=2, command=lambda: pressNumber('3'))
button5_1.place(x=20, y=290, width=50, height=35)
# ----2
button5_2 = tkinter.Button(text='2', bg='#bbbbbb',
bd=2, command=lambda: pressNumber('2'))
button5_2.place(x=77.5, y=290, width=50, height=35)
# ----1
button5_3 = tkinter.Button(text='1', bg='#bbbbbb',
bd=2, command=lambda: pressNumber('1'))
button5_3.place(x=135, y=290, width=50, height=35)
# ----减
button5_4 = tkinter.Button(text='-', bg='#708069',
bd=2, command=lambda: pressOperator('-'))
button5_4.place(x=192.5, y=290, width=50, height=35)
# ----等于
button5_5 = tkinter.Button(text='=', bg='#708069',
bd=2, command=lambda: pressOperator('='))
button5_5.place(x=250, y=290, width=50, height=80)
# --第六行
# ----0
button6_1 = tkinter.Button(text='0', bg='#bbbbbb',
bd=2, command=lambda: pressNumber('0'))
button6_1.place(x=20, y=335, width=107.5, height=35)
# ----小数点
button6_2 = tkinter.Button(text='.', bg='#bbbbbb',
bd=2, command=lambda: pressDP())
button6_2.place(x=135, y=335, width=50, height=35)
# ----加
button6_3 = tkinter.Button(text='+', bg='#708069',
bd=2, command=lambda: pressOperator('+'))
button6_3.place(x=192.5, y=335, width=50, height=35)
root.mainloop()
if __name__ == '__main__':
Demo()
2021-06-29T11:46:32 点赞:0