这不是我的全部代码,但是我已经发布了与我要执行的操作相关的内容:
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
我的目的是尝试获取按钮上的文本作为变量,以便我可以比较两个值。任何帮助将不胜感激。
答案 0 :(得分:0)
您只需要拆分按钮(Button
)和几何图形管理器(place
)的创建:
square1 = Button(window1, bg='#fbc531', fg='white', ... other options)
square1.place(x=0,y=140)
square2 = Button(window1, bg = '#4cd137', fg = 'white', ... other options)
square2.place(x=0,y=520)
square3 = Button(window1, bg = '#e84118', fg = 'white', ... other options)
square3.place(x=560,y=520)
square4 = Button(window1, bg = '#00a8ff', fg = 'white',, ... other options)
square4.place(x=560,y=140)