0

我可以制作一个功能正常但不可见的按钮吗?我查看了一堆 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()

4

3 回答 3

2

不,你不能在 tkinter 中创建一个不可见的按钮。它必须在屏幕上,用户才能单击它。

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

于 2021-06-17T23:01:40.790 回答
0

您可以将按钮 bg 和 fg 设置为与框架/根颜色相同,并设置边框宽度 = 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()
于 2021-06-18T04:07:03.373 回答
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()
于 2021-06-18T05:43:03.153 回答