个性化阅读
专注于IT技术分析

Python使用.kv文件的Kivy中的弹出窗口小部件

Kivy是Python中与平台无关的GUI工具。由于它可以在Android, IOS, Linux和Windows等操作系统上运行。它基本上是用于开发Android应用程序, 但这并不意味着它不能在桌面应用程序上使用。

??Kivy教程–通过示例学习Kivy。

弹出窗口小部件:

要使用弹出窗口, 你必须导入:

from kivy.uix.popup import Popup

弹出窗口小部件用于创建弹出窗口。默认情况下, 弹出窗口将覆盖整个”父”窗口。创建弹出窗口时, 必须至少设置Popup.title和Popup.content。

请记住, 小部件的默认大小为size_hint =(1, 1)。如果你不希望弹出窗口显示在全屏上, 则必须给出小于1的大小提示(例如size_hint =(。8, .8)), 或者停用size_hint并使用固定大小属性。

注意:弹出窗口是一个特殊的小部件。请勿尝试将其作为子级添加到任何其他小部件中。如果这样做, 将像处理普通小部件一样处理Popup, 并且不会将其隐藏在后台创建, 例如:

BoxLayout:
    MyPopup:  # bad !

基本方法:

1) import kivy
2) import kivyApp
3) import Widget
4) import Floatlayout
5) import Label
6) import popup
7) Set minimum version(optional)
8) Create widget class
9) Create Layout class :
10) create App class
9) create .kv file (name same as the app class):
        1) create Widget
        2) create popup
        3) Give label to popup
        4) create button to close popup
10) return Layout/widget/Class(according to requirement)
11) define popup function(Which shows the popup on press the button)
12) Run an instance of the class

该方法的实施:

popup.py文件

# Kivy example for the Popup widget
  
# Program to Show how to create a switch
# import kivy module   
import kivy 
     
# base Class of your App inherits from the App class.   
# app:always refers to the instance of your application  
from kivy.app import App
   
# this restrict the kivy version i.e 
# below this kivy version you cannot 
# use the app or software 
kivy.require( '1.9.0' )
  
# Widgets are elements of a graphical user
# interface that form part of the User Experience.
from kivy.uix.widget import Widget
# The Label widget is for rendering text. 
from kivy.uix.label import Label
  
# module consist the floatlayout 
# to work with FloatLayout first 
# you have to import it 
from kivy.uix.floatlayout import FloatLayout
  
# Popup widget is used to create popups.
# By default, the popup will cover
# the whole "parent" window.
# When you are creating a popup, # you must at least set a Popup.title and Popup.content.
from kivy.uix.popup import Popup
  
  
# Creating a widget class
# through this we add button
# the commands of the class is in .kv file
class Widgets(Widget):
     def btn( self ):
         # calling of the show popup function
         show_popup()
  
# Popup class is defined
# The command of the class is in .kv file
class Popups(FloatLayout):
     pass
  
# create App class
class MyApp(App):
     def build( self ):
         # return the widget
         return Widgets()
  
# define popup function in this we create the popup
def show_popup():
     show = Popups()
  
     popupWindow = Popup(title = "Popup Window" , content = show, size_hint = ( None , None ), size = ( 200 , 200 ))
  
     # open popup window
     popupWindow. open ()
  
     # Attach close button press with popup.dismiss action
     # content.bind(on_press = popup.dismiss)
   
  
# run the App 
if __name__ = = "__main__" :
     MyApp().run()

.kv文件

实现

# .kv file of the popup code
  
# Adding Button widget
<Widgets>:
     Button:
         text: "Press me"
         on_release: root.btn()
  
# Adding Label, Button to popup
<Popups>:
      
     Label:
         text: "You pressed the button"
         size_hint: 0.6 , 0.2
         pos_hint: { "x" : 0.2 , "top" : 1 }
  
     Button:
         text: "Close the popup"
         # set size of the button
         size_hint: 1 , 0.4
         # set position of the button  
         pos_hint: { "x" : 0 , "y" : 0.1 }

输出如下:

当出现按钮按下弹出窗口时, 请单击窗口中除弹出屏幕弹出窗口以外的其他任何地方, 都将消失:

Python使用.kv文件的Kivy中的弹出窗口小部件1

首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。


赞(1)
未经允许不得转载:srcmini » Python使用.kv文件的Kivy中的弹出窗口小部件

评论 抢沙发

评论前必须登录!