你能在 tkinter 中制作一个功能性的隐形按钮吗?

问题描述

我可以制作一个功能强大但不可见的按钮吗?我研究了一堆 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()

解决方法

不,您不能在 tkinter 中制作隐形按钮。它必须在屏幕上才能让用户点击。

但是,您可以对窗口中任意位置的点击做出反应,而无需创建按钮。

,

您可以将按钮 bg 和 fg 设置为与框架/根颜色相同并设置 borderwidth=0。这将创建一个不可见的按钮。 例如参考这个

ValueError: Expected n_neighbors <= n_samples,but n_samples = 1,n_neighbors = 2.
,

除了设置 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,text='and we are the dreamers of the dreams.' ) .pack()

Button( root,fg=color,activebackground=color,activeforeground=color,highlightbackground=color,borderwidth=0,command=one ) .pack()

Label( root,text='Button,button' ) .pack()
Label( root,text="who's got the button?" ) .pack()

root .bind( '<Button-1>',two )
root .mainloop()

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...