我想在python文件的特定条件下调用一个Kivy弹出窗口



我有一个函数,将显示一个主要的弹出窗口,当用户关闭弹出窗口点击任何地方的另一个弹出窗口将出现取决于传递的值作为函数的参数,问题是弹出内容的风格,当我使用.kv文件看起来很完美,但是,当我尝试使用.py创建相同的样式时文件有这么多的问题出现在我,所以有任何方法调用弹出窗口从.kv并在.py中使用它

文件?我在.py中的函数文件:

def show_popup(result):
def show_second_popup(self, *args):
if result == True:
# Show popup 1 
elif result == False:
# Show popup 2

# Show the main popup
str = Label(markup= True, text_size=(190, 160), text= "This is the main popup" , 
halign="center", valign="center", color= (0,0,0,1),font_size= 24)
popupWindow = Popup( 
auto_dismiss= True,
content= str, 
size_hint= (0.8,0.4), 
pos_hint= {'x': 0.15,'y': 0.3}, 
title= "", separator_height= 0
)

popupWindow.open()
popupWindow.bind(on_dismiss= show_second_popup)

.kv中的弹出样式文件:

<PopupTrue@Popup>
auto_dismiss: False
title: ""
separator_height: 0
size_hint: 0.8, 0.6
background_color: (0,0,0,0)
background_normal: ''


BoxLayout:
orientation: "vertical"
size: root.width, root.height
padding: 0, 10, 10, 10
border: 50
border_color: (1,1,1,1)
canvas.before:
Color:
rgba:(255/255,255/255,255/255,1)
RoundedRectangle:
pos: self.x - 20, self.y - 10
size: self.width + 40, self.height - 60
radius: [40]   
Image:
source: 'icon.png'

Label: 
text: "This popup appear if the condition was TRUE"
color: 0,0,0,1
font_size: 25


CloseButton:
text: "Close"
color: 0,0,0,1
size_hint: (None , None)
width: 105
height: 40
pos_hint: {'center_x':0.5}
on_release: root.dismiss() 

<PopupFalse@Popup>
auto_dismiss: False
title: ""
separator_height: 0
size_hint: 0.8, 0.6
background_color: (0,0,0,0)
background_normal: ''


BoxLayout:
orientation: "vertical"
size: root.width, root.height
padding: 0, 10, 10, 10
border: 50
border_color: (1,1,1,1)
canvas.before:
Color:
rgba:(255/255,255/255,255/255,1)
RoundedRectangle:
pos: self.x - 20, self.y - 10
size: self.width + 40, self.height - 60
radius: [40]   
Image:
source: 'icon.png'

Label: 
text: "This popup appear if the condition was False"
color: 0,0,0,1
font_size: 25


CloseButton:
text: "Close"
color: 0,0,0,1
size_hint: (None , None)
width: 105
height: 40
pos_hint: {'center_x':0.5}
on_release: root.dismiss() 

我想从python文件中出现主弹出窗口,并从。kv文件中调用另一个弹出窗口,因为我的。kv文件弹出窗口为按钮和弹出窗口布局提供了画布,并且我在编写画布部分时遇到了问题。

好吧,我找到了答案,我试着用一个弹出参数创建两个类,我将这些类命名为.kv文件中的名称,最后我只是用open()函数调用类。:)

class PopupTrue(Popup):
pass 

class PopupFalse(Popup):
pass 

def show_popup(result):
def show_second_popup(self, *args):
if result == True:
# Show popup 1 
PopupTrue().open()

elif result == False:
# Show popup 2
PopupFalse().open()



# Show the main popup
str = Label(markup= True, text_size=(190, 160), text= "This is the main popup" , 
halign="center", valign="center", color= (0,0,0,1),font_size= 24)
popupWindow = Popup( 
auto_dismiss= True,
content= str, 
size_hint= (0.8,0.4), 
pos_hint= {'x': 0.15,'y': 0.3}, 
title= "", separator_height= 0
)

popupWindow.open()
popupWindow.bind(on_dismiss= show_second_popup)

相关内容

  • 没有找到相关文章

最新更新