问题描述
我一直在关注 Codemy.com 的 Python-Tkinter-Paint-App-Tutorial,一切正常,直到应用程序应该截取窗口的“仅画布”部分的屏幕截图和然后将其另存为 PNG 文件。首先我得到的是完整的黑色图像,现在它只截取了它应该截取的一小部分。
我无法链接到教程,因为它是付费会员,但讲师在他的 GitHub Account 上有源代码版本。这与他在整个教程中使用的并不完全相同,但几乎相同。
当我用谷歌搜索时,它似乎是 ImageGrab 模块的一个问题,但我不明白该怎么做以及该怎么做。
该应用程序以其他方式工作,它确实保存了 PNG,但只保存在画布左上角的四分之一处(包括标题栏的一部分..)。保存时,我在终端中收到以下警告:
'2021-06-25 03:24:03.306 python[4908:221427] 警告:预期最小视图高度:(
我正在使用带有 OS big sur 的 macbook pro。 我浏览了课程网站和 Codemys Youtube Account(其中有本教程的宣传视频)上的所有评论以寻找答案,似乎还有其他一些人遇到了同样的问题。讲师的唯一提示(根据我的发现)是检查缩放设置,所以我已经这样做了。 所有依赖项似乎都已安装并正常运行。
这是我的代码:
from tkinter import *
from PIL import Image,ImageDraw,ImageTk,ImageGrab
import PIL
from tkinter import colorchooser
import tkinter.ttk as ttk
from tkinter import filedialog
from tkinter import messageBox
root = Tk()
root.title('Paint!')
root.geometry('800x800')
w = 600
h = 400
x = w/2
y = h/2
white = (255,255,255)
image1 = PIL.Image.new('RGB',(w,h),white)
draw = ImageDraw.Draw(image1)
my_canvas = Canvas(root,width=w,height=h,bg='#ffffff')
my_canvas.pack(pady=20)
# Default colors of brush and canvas
brush_color = 'black'
bg_color = 'white'
def paint(e):
# Brush parameters
brush_width = '%0.0f' % float(my_slider.get())
# Brush Types/Cap Styles: BUTT,ROUND,PROJECTING
brush_type2 = brush_type.get()
# Starting position
x1 = e.x - 1
y1 = e.y - 1
# Ending position
x2 = e.x + 1
y2 = e.y + 1
# Draw on the canvas
my_canvas.create_line(x1,y1,x2,y2,fill=brush_color,width=brush_width,capstyle=brush_type2,smooth=True)
old_x = e.x
old_y = e.y
def reset(event):
old_x = None
old_y = None
def save_as_png():
result = filedialog.asksaveasfilename(initialdir='/Users/johfa/Pictures/Paintings',filetypes=(('png files','*.png'),('all files','*.*')))
if result.endswith('.png'):
pass
else:
result = result + '.png'
# result_label = Label(root,text=result)
# result_label.pack(pady=20)
if result:
x=root.winfo_rootx()+my_canvas.winfo_x()
y=root.winfo_rooty()+my_canvas.winfo_y()
x1=x+my_canvas.winfo_width()
y1=y+my_canvas.winfo_height()
ImageGrab.grab().crop((x,y,x1,y1)).save(result)
messageBox.showinfo('Image Saved','Your Image Has Been Saved!')
my_canvas.bind('<B1-Motion>',paint)
def clear_screen():
my_canvas.delete(ALL)
my_canvas.config(bg='white')
def change_brush_color():
global brush_color
brush_color = 'black'
brush_color = colorchooser.askcolor(color=brush_color)[1]
# color = Label(root,text=brush_color)
# color.pack(pady=20)
def change_canvas_color():
global bg_color
bg_color = 'white'
bg_color = colorchooser.askcolor(color=bg_color)[1]
my_canvas.config(bg=bg_color)
def change_brush_size(command_thing):
slider_label.config(text='%0.0f' % float(my_slider.get()))
# Create brush options frame
brush_options_frame = Frame(root)
brush_options_frame.pack(pady=20)
# Brush size
brush_size_frame = LabelFrame(brush_options_frame,text='Brush Size')
brush_size_frame.grid(row=0,column=0,padx=50)
# Brush slider
my_slider = ttk.Scale(brush_size_frame,from_=1,to=100,command=change_brush_size,orient=VERTICAL,value=10)
my_slider.pack(pady=10,padx=10)
# Brush slider label
slider_label = Label(brush_size_frame,text=my_slider.get())
slider_label.pack(pady=5)
# Brush type
brush_type_frame = LabelFrame(brush_options_frame,text='Brush Type',height=400)
brush_type_frame.grid(row=0,column=1,padx=50)
# Create string variable for brush type to set and get
brush_type = StringVar()
brush_type.set('round')
# Create radio buttons for brush type
brush_type_radio1 = Radiobutton(brush_type_frame,text='Round',variable=brush_type,value='round')
brush_type_radio2 = Radiobutton(brush_type_frame,text='Slash',value='butt')
brush_type_radio3 = Radiobutton(brush_type_frame,text='Diamond',value='projecting')
# Put the radio buttons on the screen
brush_type_radio1.pack(anchor=W)
brush_type_radio2.pack(anchor=W)
brush_type_radio3.pack(anchor=W)
# Change color
change_colors_frame = LabelFrame(brush_options_frame,text='Change Color')
change_colors_frame.grid(row=0,column=2)
# Change brush color button
brush_color_button = Button(change_colors_frame,text='Brush Color',command=change_brush_color)
brush_color_button.pack(pady=10,padx=10)
# Change canvas color button
canvas_color_button = Button(change_colors_frame,text='Canvas Color',command=change_canvas_color)
canvas_color_button.pack(pady=10,padx=10)
# The program options frame
options_frame = LabelFrame(brush_options_frame,text='Options')
options_frame.grid(row=0,column=3,padx=50)
# Create clear button
clear_btn = Button(options_frame,text='Clear Screen',command=clear_screen)
clear_btn.pack(padx=10,pady=10)
# Create save image button
save_image_btn = Button(options_frame,text='Save as PNG',command=save_as_png)
save_image_btn.pack(pady=10,padx=10)
root.mainloop()
我没有得到什么? 提前致谢!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)