你好世界! 首先,我是这个页面英文版的新手。我认为我的英语很好,但如果有任何错误,请告诉我,我会改正。其次,我编写的代码(稍后我将向您展示)可能不是最有效的,但我只希望它能够工作。事情是这样的...
我正在与 kivy 一起练习,我决定制作一个程序,该程序将从单词列表中创建一个字谜,用户必须猜测单词,程序将判断它是否正确。我做了一个程序来做到这一点,但是我遇到了“新词”按钮的问题(可以说是一个新游戏),所以我决定制作一个新代码(我会在一条评论)。而这个新代码从一开始就失败了。当我添加标签和按钮时,我不仅无法点击它们,而且它们还显示在屏幕的左下方,而且非常小。图像显示结果:Labels,Buttons and RelativeLayout。
from kivy.app import App
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from random import choice,shuffle
from kivy.core.window import Window
Window.clearcolor = (.10,.54,1,1)
Window.size = (500,300)
# ------- THIS PART SHOULD GO ON A SEPARATE FILE ----------------------------
list_of_words = ['ELEPHANT','LION','COCODRILE','MONKEY','KANGAROO']
def create_anagram():
# Takes a word from the list and shuffles the letters to create the anagram
choices = choice(list_of_words)
list_letters = []
for item in choices:
list_letters.append(item)
shuffle(list_letters)
choices = ''.join(list_letters)
return choices
# ----------------------------------------------------------------------------
class Anagram(RelativeLayout):
def __init__(self,**kwargs):
super(Anagram,self).__init__(**kwargs)
# These are all variables which will be used later on.
self.choices = create_anagram()
self.window = RelativeLayout()
self.font_size = 20
self.letters,self.underscores = [],[]
self.ind = 0 # Index used on chose_letter()
self.size_x,self.size_y = 100,50 # Sizes for the labels and button
self.pos_x = 0 # Initial position for the labels and buttons
self.word = '' # will concatonate the letters choosen
self.new_game()
def new_game(self):
with self.canvas.after:
for item in self.choices:
self.letters.append(Button(text=item,size_hint=(1/len(self.choices),0.2),pos_hint = {'x': self.pos_x,'top': .8},font_size = self.font_size,color='#688FF8',background_color = '#1F48F2',disabled = False))
self.underscores.append(Label(text='_','top': 0.4},size=(self.size_x,self.size_y),color = '#FFFFFF'))
self.pos_x += 1/len(self.choices)
# Adding buttons for the letters and the underscores.
for item in self.letters:
self.window.add_widget(item)
for item in self.underscores:
self.window.add_widget(item)
class MainAnagramApp(App):
def build(self):
return Anagram()
if __name__ == '__main__':
MainAnagramApp().run()'''
就是这样。这就是代码和问题。知道为什么会发生这种情况吗?
顺便说一下,我试过没有“with self.canvas”,但它甚至没有显示小部件。我也尝试过更改布局(Grid、Anchor 等),但问题完全相同。
谢谢!