开发者社区> 问答> 正文

如何在Tkinter中获取按钮的文本

这不是我的全部代码,但是我发布了与我要执行的操作有关的内容:

from tkinter import *
from random import seed
from random import randint

class screens:
    def __init__(self, title, size, bg):
        self.title = title
        self.size = size
        self.bg = bg

def if_right4():
    if square4["text"] == 'hi':
        print('Yes')
    else:
        print("No")

def screen_one():

    window1 = Tk()

    global square1
    global square2
    global square3
    global square4

    window1.title(s1.title) # Sets window title
    window1.geometry(s1.size) # Sets window size
    window1.configure(bg=s1.bg) # Changes background
    window1.iconbitmap(default='Images/favicon.ico') # Removes the 'feather'
    window1.resizable(0,0)
#     Button(object1, text="Enter", width=15, height=2, command=quit).place(x=0, y=0)

    TopFrame = Frame(window1, bg = '#273c75', width = 950, height = 50).place(x=0,y=0)

    TopFrame2 = Frame(window1, bg = '#192a56', width = 950, height = 100).place(x=0,y=40)


    streakLabel = Label(window1, fg = 'black',  bg = '#ffffff', font = 'Helvetica 20 bold', text = 'Streak:').place(x=400,y=430)
    displayStreak = Label(window1, fg = 'black',  bg = '#ffffff', font = 'Helvetica 22 bold', text = 0).place(x=435,y=470)
    displayQuestion = Label(window1, fg = '#ffffff',  bg = '#273c75', font = 'Helvetica 22 bold', text = displayedQuestion).place(x=0,y=0)



    square1 = Button(window1, bg = '#fbc531', fg = 'white', width = 34, height = 17, text = displayedOption1, font = 'Helvetica 12 bold',
                    bd=0, highlightthickness=0 ).place(x=0,y=140)

    square2 = Button(window1, bg = '#4cd137', fg = 'white', width = 34, height = 17, text = displayedOption2, font = 'Helvetica 12 bold', 
                     bd=0, highlightthickness=0 ).place(x=0,y=520)

    square3 = Button(window1, bg = '#e84118', fg = 'white', width = 34, height = 17, text = displayedOption3, font = 'Helvetica 12 bold', 
                     bd=0, highlightthickness=0 ).place(x=560,y=520)

    square4 = Button(window1, bg = '#00a8ff', fg = 'white', width = 34, height = 17, text = displayedOption4, font = 'Helvetica 12 bold', 
                     bd=0, highlightthickness=0, command=if_right4).place(x=560,y=140)


    window1.mainloop()

“ displayedOption”变量等于文本文件中的随机行。

我得到的错误是TypeError: 'NoneType' object is not subscriptable 我的目标是尝试获取按钮上的文本作为变量,以便我可以比较两个值。任何帮助,将不胜感激。

展开
收起
几许相思几点泪 2019-12-23 21:51:20 1367 0
0 条回答
写回答
取消 提交回答
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载