python - 初学者 Python 键盘 GUI 设置

标签 python user-interface tkinter keyboard

我开始使用 Python 3.5 中的 GUI,我正在尝试设置一个简单的 qwerty 键盘。基于示例,我尝试了以下代码

from tkinter import Tk, Label, RAISED, Button, Entry

self.window = Tk()

    #Keyboard
    labels = [['q','w','e','r','t','y','u','i','o','p'],
                 ['a','s','d','f','g','h','j','k','l'],
                 ['z','x','c','v','b','n','m','<']]

    n = 10
    for r in range(3):
        for c in range(n):
            n -= 1
            label = Label(self.window,
                              relief=RAISED,
                              text=labels[r][c])
            label.grid(row=r,column=c)
            continue

这给了我第一行,但它没有返回任何其他内容。我尝试简单地使用 10 作为范围,它创建了键盘的前两行,但它仍然没有继续到最后一行。

最佳答案

您的问题在行 n -= 1 中。每次创建标签时,您都会让 n 少一个 - 在第一整行之后,n==0,因此范围是 0>0,并且范围从不包含上限 - for c in range(0) 将退出循环(因为它已经遍历了所有不存在的内容)。

更好的解决方案是遍历列表而不是索引 - for 循环接受任何可迭代对象(列表、字典、范围、生成器、集合等);

for lyst in labels: 
    # lyst is each list in labels
    for char in lyst:
        # char is the character in that list
        label = Label(... text=char) # everything else in the Label() looks good.
        label.grid(...) # You could use counters for this or use ennumerate()-ask if you need.
        # The continue here was entirely irrelevant.

关于python - 初学者 Python 键盘 GUI 设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38229742/

相关文章:

python - 如何在 Python 的 SciPy 中删除稀疏矩阵中的小元素?

python - 在 numpy 数组中找到最大上三角项的索引的有效方法?

python - 用于触摸屏设备的 pygtk

python - 在 Python3 中选择一个文件

python - 将滚动条放在 Canvas 上的框架上

python - 如何捕获 tkinter 子部件上的事件?

python - 在一系列列表中查找缺失的整数

python - Python 中的 %i 和 %d 有什么区别?

python - Python是否可以在文本框中实时显示LaTex?

android - 如何为 Android 应用程序创建类似 'Pulse' 的 UI