【问题标题】:Tkinter - Changing all Button text alphabets to uppercase on a Button clickTkinter - 在单击按钮时将所有按钮文本字母更改为大写
【发布时间】:2021-01-28 14:48:33
【问题描述】:

我正在使用 Tkinter 制作一个屏幕键盘,我想实现一个“Shift”按钮,以便在点击时将字母大写,就像在真正的键盘上一样。 我想知道如何实现这个功能?

在单击 Shift 按钮之前,我的列表应该是这样的

buttons = [
    '!', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '←', '7', '8', '9', '-',
    'Tab', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', '[', ']', '4', '5', '6', '+',
    'Shift', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', '?', '*', '1', '2', '3',
    ' Space ' 
]

单击 shift 按钮后,我希望我的按钮列表看起来像

shift_buttons = [
    '!', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '←', '7', '8', '9', '-',
    'Tab', 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', '[', ']', '4', '5', '6', '+',
    'Shift', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', ',', '.', '/', '?', '*', '1', '2', '3',
    ' Space ' 
]

简而言之,如何仅通过单击 Button 小部件将 Button 文本从第一个列表更改为第二个列表?

【问题讨论】:

  • 按钮代码在哪里,也请附上
  • GitHub link 这是我现在的全部代码
  • 请在您的问题中输入您的代码。
  • 代码没有任何内容可以显示您为实现shift 按钮单击所做的尝试。
  • 我建议不要提供如此复杂的代码让我们了解正在做的事情,制作另一个与您的情况相似的示例并将其包含在内,以便我们提供帮助。基本上你会想要绑定到窗口,每当按下'Shift' 时,你想使用config() 更改按钮的文本,但不确定如何在那里定义按钮。

标签: python python-3.x tkinter


【解决方案1】:

根据 Github 链接中的代码,建议:

  • 定义一个变量shift_on来保存Shift键的当前状态
  • 定义一个列表letter_buttons 来保存字母按钮的引用('a' 到 'z')
shift_on = False
letter_buttons = []

然后修改buttonClick()如下:

def is_letter(s):
    return len(s) == 1 and 'a' <= s <= 'z'

def buttonClick(input):
    global shift_on
    if input == 'Shift':
        # toggle shift state
        shift_on = not shift_on
        # update the text of the letter buttons
        for btn in letter_buttons:
            text = btn['text']
            btn['text'] = text.upper() if shift_on else text.lower()
    else:
        if input == ' Space ':
            textBox.insert(INSERT,' ')
        elif input == 'Tab':
            textBox.insert(INSERT, '    ')
        elif input == '←':
            backspace()
        else:
            if is_letter(input):
                input = input.upper() if shift_on else input.lower()
            textBox.insert(INSERT, input)

并修改for循环以创建如下按钮:

for button in buttons:
    # command to run on every button click - buttonClick() function 
    cmd = lambda x = button: buttonClick(x)

    # for every button except 'space'
    if button != ' Space ':
        btn = tk.Button(keyboardApp,
            text = button, 
            width = 7,
            bg = 'black',
            fg = 'white',
            activebackground = 'white',
            activeforeground = 'black',
            relief = 'raised',
            padx = 3,
            pady = 3, 
            bd = 5,
            font=('arial', 12, 'bold'),
            command = cmd)
        btn.grid(row = varRow, column = varCol)
        # save the reference of letter buttons
        if is_letter(button):
            letter_buttons.append(btn)
    ...

【讨论】:

  • 非常感谢@acw1668。这正是我想要我的代码做的!高度赞赏:)
【解决方案2】:

您应该首先创建一个进行转换的函数。例如,从您的代码中定义一个执行类似操作的函数

def shift_buttons():
    buttons = shift_buttons

注意:按钮和 shift_buttons 是指将普通按钮从键盘更改为屏幕键盘中的大写按钮。希望你明白我的意思。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-17
    • 1970-01-01
    相关资源
    最近更新 更多