我正在嘗試用 Python 和 Kivy 制作 tambola 硬幣拾取器,我是 kivy 的新手。在這里,我創(chuàng)建了從 1 到 90 的網(wǎng)格布局按鈕。我想在選擇數(shù)字時(shí)更改網(wǎng)格布局中特定按鈕的顏色。我在使用新的彩色按鈕更新網(wǎng)格布局時(shí)遇到問(wèn)題。#!/usr/bin/pythonfrom kivy.app import Appfrom kivy.uix.button import Buttonfrom kivy.uix.label import Labelfrom kivy.uix.floatlayout import FloatLayoutfrom kivy.uix.gridlayout import GridLayout?from kivy.graphics import Colorimport randomcoins = random.sample(range(1,91), 90)#print(coins)picked_coins=[]current_coin=0#print(picked_coins)class Housie(FloatLayout):? ? def __init__(self,**kwargs):? ? ? ? super(Housie,self).__init__(**kwargs)? ? ? ? self.title = Label(text="Housie Coin Picker",font_size = 50,size_hint=(1, .55),pos_hint={'x':0, 'y':.45})? ? ? ? self.main_label = Label(text = "Click PICK NUMBER", size_hint=(1, .60),pos_hint={'x':0, 'y':.35})? ? ? ? self.picked_ones = Label(text = "picked_coins", size_hint=(1, .40),pos_hint={'x':0, 'y':.40})? ? ? ? self.help_button = Button(text = "PICK NUMBER", size_hint=(.3, .1),pos_hint={'x':.65, 'y':.1},on_press = self.update)? ? ? ? self.add_widget(self.title)? ? ? ? self.add_widget(self.main_label)? ? ? ? self.add_widget(self.picked_ones)? ? ? ? self.add_widget(self.help_button)? ? ? ? self.add_widget(self.userinterface())? ? def userinterface(self):? ? ? ? self.layout = GridLayout(cols = 10,size_hint=(.50, .50))? ? ? ? for i in range(1,91):? ? ? ? ? ? self.layout.add_widget(Button(background_color=(1,0,0,1),text =str(i)))? ? ? ? return self.layout? ? def update(self,event):? ? ? ? for coin in coins:? ? ? ? ? ? if coin not in picked_coins:? ? ? ? ? ? ? ? current_coin=coin? ? ? ? ? ? ? ? picked_coins.append(coin)? ? ? ? ? ? ? ? self.main_label.text = str(coin)? ? ? ? ? ? ? ? for i in self.layout.children:? ? ? ? ? ? ? ? ? ? if i.text == str(coin):? ? ? ? ? ? ? ? ? ? ?#What to do Here?
1 回答

鴻蒙傳說(shuō)
TA貢獻(xiàn)1865條經(jīng)驗(yàn) 獲得超7個(gè)贊
您可以Button像這樣將一個(gè)方法綁定到每個(gè)方法:
def userinterface(self):
self.layout = GridLayout(cols = 10,size_hint=(.50, .50))
for i in range(1,91):
self.layout.add_widget(Button(background_color=(1,0,0,1),text=str(i), on_release=self.butt_pressed))
return self.layout
def butt_pressed(self, button):
button.background_normal = ''
button.background_color = (1,0,0,1)
Thbutt_pressed()方法更改了 pessed 的背景顏色Button。
添加回答
舉報(bào)
0/150
提交
取消