我可以制作一个功能强大但不可见的按钮吗?我研究了一堆 Tkinter 线程,但是当我测试代码时,它们都导致按钮完全消失并被禁用。这是我到目前为止所得到的:
import tkinter as tk
import time
app=tk.Tk()
app.minsize(500, 500)
#button function
def move():
button.config(state='disabled')
for i in range(50):
frame.place(x=250+i)
frame.update()
time.sleep(0.01)
frame.place(x=250, y=250)
button.config(state='normal')
block=tk.Frame(app, height=50, width=50, bg='red')
frame=tk.Frame(app, height=400, width=400, bg='blue')
#button I wish to be invisible
button=tk.Button(app, text='clickme', command=move)
button.place(x=40, y=40)
frame.place(x=250, y=250, anchor='center')
block.place(x=50, y=50)
app.mainloop()
答案 0 :(得分:2)
不,您不能在 tkinter 中制作隐形按钮。它必须在屏幕上才能让用户点击。
但是,您可以对窗口中任意位置的点击做出反应,而无需创建按钮。
答案 1 :(得分:0)
您可以将按钮 bg 和 fg 设置为与框架/根颜色相同并设置 borderwidth=0。这将创建一个不可见的按钮。 例如参考这个
ValueError: Expected n_neighbors <= n_samples, but n_samples = 1, n_neighbors = 2.
答案 2 :(得分:0)
除了设置 fg 和 bg 之外,还有更多内容,但是是可以做到的。
#! /usr/bin/env python3
from tkinter import *
color = 'SlateGray4'
width, height = 300, 150
desiredX, desiredY = width *0.5, height *0.75
root = Tk() ; root .title( 'Snozberries' )
root .geometry( f'{width}x{height}' )
root .bind( '<Escape>', lambda e: root .destroy() )
root .configure( bg = color )
def one(): ## takes many parameters during construction
print( 'Found a button!' )
def two( event ):
## print( f'x: {event .x}, y: {event.y}' )
if event .x >= desiredX -25 and event .x <= desiredX +25 \
and event .y >= desiredY -25 and event .y <= desiredY +25:
print( 'Found another button!' )
Label( root, bg=color, text='We are the musicmakers,' ) .pack()
Label( root, bg=color, text='and we are the dreamers of the dreams.' ) .pack()
Button( root, bg=color, fg=color, activebackground=color, activeforeground=color,
highlightbackground=color, borderwidth=0, command=one ) .pack()
Label( root, bg=color, text='Button, button' ) .pack()
Label( root, bg=color, text="who's got the button?" ) .pack()
root .bind( '<Button-1>', two )
root .mainloop()