简体   繁体   English

Python & Tkinter。 我希望循环中的一个按钮在按下一次时变为红色并保持红色

[英]Python & Tkinter. I would like one of my buttons in my loop to change red & stay red when pressed once

Good morning,早上好,

I have have a little query & don't seem to be able to see anything on stack overflow which answers my question.我有一个小问题,似乎无法在堆栈溢出上看到任何回答我的问题的东西。 I have attached a test script below.我在下面附上了一个测试脚本。 What I would like to happen is when the "Shift" button in my loop is pressed the background of the "Shift" button goes red & stays red.我想要发生的是当按下循环中的“Shift”按钮时,“Shift”按钮的背景变为红色并保持红色。 However if the shift button is pressed again I would like the background of the "Shift" button to go back to white.但是,如果再次按下 shift 按钮,我希望 go 的“Shift”按钮的背景变回白色。

Thank you in advance, see the test code below.在此先感谢您,请参阅下面的测试代码。

import tkinter as tk

window = tk.Tk()
CommandList = ["BckSpace", "Shift", "Space", "Enter"]
ShiftKey = False

for i in range (len(CommandList)):
    CommandBtn = tk.Button(window, width= 5, height = 1, text = CommandList[i], command = lambda x = i: CommandFunc(x))
    CommandBtn.pack()


def CommandFunc(x):
    global ShiftKey
    if x == 0:
        pass

    if x == 1:
        if ShiftKey == False:
            ShiftKey = True
            #Background of only button "shift" to go red and stay red
            

        else:
            ShiftKey = False
            # Backgroud of button "shift" to go back to white

    if x == 2:
        pass

    if x == 3:
        pass



window.mainloop()

You should be passing the button instance not index to your CommandFunc .您应该将按钮实例而不是索引传递给您的CommandFunc Then you can use button['text'] to check if the shift key was pressed.然后您可以使用button['text']检查是否按下了 shift 键。

Here is an example这是一个例子

import tkinter as tk

window = tk.Tk()
command_list = ["BckSpace", "Shift", "Space", "Enter"]
ShiftKey = False

for i in command_list:
    commandBtn = tk.Button(window, width= 5, height = 1, text = i)
    commandBtn.config(command= lambda x=commandBtn: CommandFunc(x))
    commandBtn.pack()


def CommandFunc(btn):
    global ShiftKey
    
    if btn['text'] == command_list[1]:
        if ShiftKey == False:  # or btn['bg'] == 'red'
            ShiftKey = True
            btn['bg'] = 'red'
            

        else:
            
            ShiftKey = False
            btn['bg'] = 'white' # or 'SystemButtonFace' if you want the orginal color


window.mainloop()

A small suggestion would be to name your variables and function following PEP guidelines一个小建议是按照 PEP 指南命名您的变量和 function

暂无
暂无

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

相关问题 在通过tkinter创建带有for循环的按钮时遇到了麻烦。 (蟒蛇) - Having trouble creating buttons with a for-loop through tkinter. (Python) 为什么按下 guizero 按钮时我的 LED 灯不亮? - Why don't my LEDS stay on when the guizero buttons are pressed? 我正在使用 Tkinter 在 Python 中制作剪刀石头布游戏。 我的分数工作不正常 - I'm Making a Rock Paper Scissors game in Python with Tkinter. My Score isn't Working Properly 使用tkinter在我的python代码中遇到问题。 我不断收到一条消息,指出对象没有属性 - Having problems with my python code, using tkinter. I keep getting a message that says object has no attribute 为什么我的按钮在我重新打开时重复(python tkinter) - why are my buttons duplicating when i reopen them (python tkinter) 使用tkinter 为Python 程序创建UI。Tkinter 中的按钮可以多次按下。 如何创建“一个过程”弹出窗口? - Creating a UI for Python Program using tkinter. The button in Tkinter can be pressed multiple times. How can I create a "One Process" Pop Up? 我如何使用 Tkinter 从 python 中的另一个文件访问 function。 我找不到任何解决我的具体问题的方法 - How do i access a function from another file in python with Tkinter. I can't find any solution to my specific problem 使用 Tkinter 创建 UI。 尝试调整按钮文本的字体并运行程序时,我收到此错误 - Creating a UI with Tkinter. When trying to adjust the font of my button text and I run the program, I get this error back Python-Tkinter。 为什么我的tkinter条目不连接到列表中的最后一个变量? - Python - Tkinter. Why doesn't my tkinter entry connect to the last variable in the list? 找不到方法,当我按下鼠标单击时,可以让我的一张照片停留在屏幕上 - Can't find a way to get one of my pictures to stay on screen when my mouse click is pressed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM