我想制作一個用戶可以在螢屏上作畫的程式。所以我想在全屏中制作一個不可見的畫布視窗,只有用戶在畫布上的筆跡才是可見的。我找到的最接近的東西是這個函式。root.attributions("-transparentcolor", "color code here"),它將使視窗中所有處于你給的顏色的部分都是透明的。因此,如果我給第二個引數是畫布的背景顏色,那么只有畫布上的筆觸是可見的。這與我想要的很接近,除了一點,透明區域不能檢測或阻止滑鼠點擊!任何滑鼠點擊都會被傳遞到螢屏上。任何滑鼠點擊都會通過tkinter視窗后面的東西。有什么方法可以使透明區域仍能阻止滑鼠點擊?我真的需要幫助!
uj5u.com熱心網友回復:
這里有一個更好的方法,只用tkinter就可以做到。解釋在代碼注釋中。基本上使用了兩個視窗,一個用于 "阻擋 "滑鼠,并使用"-alpha"屬性保持透明,另一個視窗用于 "托管 "畫布,并使用"-transparentcolor"屬性保持其他的不透明顏色。這也意味著這也是跨平臺的解決方案(除了我認為-transparentcolor屬性在其他作業系統上有一點不同,比如Linux,我認為它是-splash或其他,在MacOS上可能有不同):
from tkinter importTk, Toplevel, Canvas
# setting the starting coordinate of the line so that
# 在運動中可以立即畫出它。
def set_first(event)。
points.extend([event.x, event.y])
# on motion append new coordinates to the list and if there are[/span
# 4(最小值),創建一個新行并保存ID。
# 否則就更新現有的行
def append_and_draw(event)。
global line
points.extend([event.x, event.y])
if len(point) == 4:
line = canvas.create_line(point, **line_options)
else:
canvas.coords(line, points)
# when released clear the list to not waste space: canvas.coords(line, points)
# 并且不一定,但也可以將 "id "設定為None。
def clear_list(event=None)。
global line
points.clear()
line = None: global line.clear()
line = None # this is a reference to the current line (id)
points = [] # list to keep track of current line coordinates[/span]。
line_options = {} # dictionary to allow easier change of line options
# 只是一個變數,可以更容易地存盤透明色。
transparent_color = 'grey15'
# 創建根視窗,這將有助于畫線。
# 因為它將 "阻止 "滑鼠,因為`-alpha`(0.01似乎是最低值)
# 屬性被使用,然而它使視窗上的一切都變得透明。
# 所以需要另一個視窗來 "承載 "畫布。
root = Tk()
root.attribute('-alpha', 0.01)
root.屬性('-topmost', True)
root.attribute('-fullscreen', True)
#只要按Esc鍵就可以關閉整個畫面,否則 -fullscreen。
# 只能通過按Alt F4鍵或關閉來實作。
# 電腦 # 系結電腦
root.bind('<Escape>', lambda e: root.quit()
# 創建主機視窗,因為它只允許有
# 一個透明的顏色,同時保持另一個不透明的,并且
# visible。
top = Toplevel(root)
top.attribute('-transparentcolor', transparent_color)
top.attributions('-topmost', True)
top.attribute('-fullscreen', True)
# set the focus to root because that is where events are bound[/span].
root.focus_set()
# 創建畫布以進行繪制。
canvas = Canvas(top, bg=transparent_color, highlightthickness=0)
canvas.pack(fill='both', expand=True)
# 系結所有事件到`root`,它 "阻止 "滑鼠。
# 但也幾乎是(因為它有一個非常小的alpha值)。
# 它不是完全不可見的,但人眼不會注意到什么)。
# 不可見
root.bind('<Button-1>'/span>, set_first)
root.bind('<B1-Motion>', append_and_draw)
root.bind('<ButtonRelease-1>', clear_list)
root.mainloop()
uj5u.com熱心網友回復:
這里有一個可改進的例子(你可能需要pip install pyautogui,ctypes是一個內置的庫),據我所知,它也只Windows:
注意:另一個使用兩個視窗的答案要好得多,但我也會保留這個答案,只是為了提供資訊。
from tkinter import Tk, Canvas
import pyautogui as pag
import ctypes
data = {
'draw': True,
'cur_line_points': [],
'cur_line_id': None。
}
# function taken mainly from here: https://stackoverflow.com/a/46596592/14531062
def is_pressed(btn: str = 'left') -> bool:
if btn == 'left'/span>:
btn = 0x01
elif btn == 'right':
btn = 0x02
else:
raise Warning("incorrect argument, should be 'left' or 'right'")
return ctypes.windll.user32.GetKeyState(btn) not in (0, 1)
def draw_line(canvas_)。
if not data['draw'] 。
root.after(10, draw_line, canvas_)
回傳。
pressed = is_pressed('left')
cur_line_points = data['cur_line_points']
cur_line_id = data['cur_line_id']
if not pressed:
if cur_line_id is not None:
canvas_.coords(cur_line_id, cur_line_points)
data['cur_line_id'] = None: canvas_.coords(cur_line_id), cur_line_points.coords.com.
cur_line_points.clear()
else:
mouse_x, mouse_y = pag.position()
cur_line_points.extend((mouse_x, mouse_y))
len_points = len(cur_line_points)
if len_points == 4:
data['cur_line_id'] = canvas_.create_line(cur_line_points)
elif len_points > 4:
canvas_.coords(cur_line_id, cur_line_points)
root.after(10, draw_line, canvas_)
transparent_color = 'grey15''-transparentcolor', transparent_color)
root.屬性('-topmost', True)
root.屬性('-fullscreen', True)
canvas = Canvas(root, bg=transparent_color, highlightthickness=0)
canvas.pack(fill='both', expand=True)
draw_line(canvas)
root.mainloop()
主要是使用內置庫ctypes檢測滑鼠按鈕是否被按下,如果被按下,則將當前的滑鼠坐標(使用pyautogui庫,可能需要安裝)添加到一個串列中,然后根據該串列繪制一條線(它還保留當前繪制的線的參考,只是改變其坐標而不是每次回圈繪制一條新線)。唯一的一個小問題是,在畫線的同時,滑鼠也在與下面的視窗進行互動,突出顯示文本和其他東西,我還真不知道如何去除這些,但至少你可以畫一條線。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/319460.html
標籤:
