繁体   English   中英

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

[英]Can you make a functional invisible button in 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 中创建一个不可见的按钮。它必须在屏幕上,用户才能点击它。

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

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

from tkinter import *
root = Tk()

masterFrame = Frame(root, bg='orange', height=300, width=600)
masterFrame.grid(row=0, column=0, sticky='nsew')
Button(masterFrame, text="Invisible button", bg='orange', fg='orange', borderwidth=0).pack()
root.mainloop()

除了设置 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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM