【问题标题】:Binding function from other class to Kivy Button从其他类绑定函数到 Kivy Button
【发布时间】:2017-09-18 20:52:43
【问题描述】:

Kivy app screenshot 我在同一个项目中有 2 个 .py 文件,名为 dailyReports.py 和 weekReports.py,它们每个都生成一个不同的 Excel 表,一个是每日报告,另一个是每周报告。当我单击运行时,它会直接打开我想要的 excel 文件。 我正在尝试制作一个简单的用户友好界面,允许用户选择他们想要生成的报告,并让它运行相应的报告生成器并输出 excel 文件。这是我第一次处理 python 和 kivy,但到目前为止,我能够使我的两个报告生成器工作,并且我的 kivy GUI 的视觉方面正是我想要的。我的问题是当我尝试将下拉菜单中的两个按钮绑定到它们相应的 excel 生成器函数时。有人可以帮我吗?这是迄今为止我的 .py 文件的代码,其中包含我的 kivy 代码以及当我单击下拉按钮时我当前的 kivy GUI 的样子的图片。 我真的很感激!!!!

    from kivy.uix.label import Label
    from kivy.uix.dropdown import DropDown
    from kivy.app import App
    from kivy.graphics import Color, Rectangle
    from kivy.uix.floatlayout import FloatLayout
    from kivy.graphics.context_instructions import Color
    from kivy.config import Config
    from kivy.uix.image import Image
    from kivy.uix.button import Button
    dropdown = DropDown()

    class Panel(FloatLayout):

        def __init__(self, **kw):
            super(Panel, self).__init__(**kw)

            # Make the background solid white
            color = Color(1.0, 1.0, 1.0, 1.0)
            self.canvas.add(color)
            rect = Rectangle(pos=self.pos, size=self.size)
            self.canvas.add(rect)

            dropdown = DropDown()

            # Add the label
            label = Label(text="Report Generator", pos_hint={'x': 0, 'y': 0.20}, size_hint=(None, None))
            label.color = [0.0, 0.0, 0.0, 1.0]
            label.size_hint = (1, 1)
            label.font_size = 26
            label.bold = True
            self.add_widget(label)

            choice = Button(text="Select Summary Report to Generate", pos_hint={'center_x': .5, 'center_y': .58},
                    size_hint=(0.6, 0.2))
            choice.color = [1.0, 1.0, 1.0, 1.0]
            choice.size_hint = (0.45, 0.1)
            choice.font_size = 18
            choice.bold = False
            choice.bind(on_release=dropdown.open)
            self.add_widget(choice)

            btn1 = Button(text='Daily Report', font_size=18, size_hint_y=None, height=60)
            btn1.color = [1, 1, 1, 1]
            btn1.txt= 'Generating Daily Summary Report...'
            btn1.bind(on_release=lambda btn1: dropdown.select(btn1.txt))
            dropdown.add_widget(btn1)
            btn2 = Button(text='Weekly Report', font_size=18, size_hint_y=None, height=60)
            btn2.color = [1, 1, 1, 1]
            btn2.txt = 'Generating Weekly Summary Report...'
            btn2.bind(on_release=lambda btn2: dropdown.select(btn2.txt))
            dropdown.add_widget(btn2)
            dropdown.bind(on_select=lambda instance, x: setattr(choice, 'text', x))


    class Report(App):
       def build(self):[enter image description here][1]
            Config.set('graphics', 'width', '450')
            Config.set('graphics', 'height', '250')
            return Panel(size=(1000,1000))


    # Application Code
    if __name__ == '__main__':
        Report().run()

【问题讨论】:

    标签: python windows user-interface pycharm kivy


    【解决方案1】:

    您已按照 sn-p 添加以下代码。请参阅下面的示例以获取完整的详细信息。

    片段

    from DailySummaryReport import DailySummary
    from WeeklySummaryReport import WeeklySummary
    ...
            btn1.bind(on_release=lambda btn1: dropdown.select(btn1.txt))
            btn1.bind(on_release=lambda btn1: DailySummary())
    ...
            btn2.bind(on_release=lambda btn2: dropdown.select(btn2.txt))
            btn2.bind(on_release=lambda btn2: WeeklySummary())
    

    示例

    main.py

    ​​>
    from DailySummaryReport import DailySummary
    from WeeklySummaryReport import WeeklySummary
    
    from kivy.uix.label import Label
    from kivy.uix.dropdown import DropDown
    from kivy.app import App
    from kivy.graphics import Color, Rectangle
    from kivy.uix.floatlayout import FloatLayout
    from kivy.graphics.context_instructions import Color
    from kivy.config import Config
    from kivy.uix.image import Image
    from kivy.uix.button import Button
    
    
    class Panel(FloatLayout):
        def __init__(self, **kw):
            super(Panel, self).__init__(**kw)
            self.size = (1000, 1000)
            # Make the background solid white
            color = Color(1.0, 1.0, 1.0, 1.0)
            self.canvas.add(color)
            rect = Rectangle(pos=self.pos, size=self.size)
            self.canvas.add(rect)
    
            dropdown = DropDown()
    
            # Add the label
            label = Label(text="Report Generator", pos_hint={'x': 0, 'y': 0.20}, size_hint=(None, None))
            label.color = [0.0, 0.0, 0.0, 1.0]
            label.size_hint = (1, 1)
            label.font_size = 26
            label.bold = True
            self.add_widget(label)
    
            choice = Button(text="Select Summary Report to Generate", pos_hint={'center_x': .5, 'center_y': .58},
                            size_hint=(0.6, 0.2))
            choice.color = [1.0, 1.0, 1.0, 1.0]
            choice.size_hint = (0.45, 0.1)
            choice.font_size = 18
            choice.bold = False
            choice.bind(on_release=dropdown.open)
            self.add_widget(choice)
    
            btn1 = Button(text='Daily Report', font_size=18, size_hint_y=None, height=60)
            btn1.color = [1, 1, 1, 1]
            btn1.txt = 'Generating Daily Summary Report...'
            btn1.bind(on_release=lambda btn1: dropdown.select(btn1.txt))
            btn1.bind(on_release=lambda btn1: DailySummary())
            dropdown.add_widget(btn1)
            btn2 = Button(text='Weekly Report', font_size=18, size_hint_y=None, height=60)
            btn2.color = [1, 1, 1, 1]
            btn2.txt = 'Generating Weekly Summary Report...'
            btn2.bind(on_release=lambda btn2: dropdown.select(btn2.txt))
            btn2.bind(on_release=lambda btn2: WeeklySummary())
            dropdown.add_widget(btn2)
            dropdown.bind(on_select=lambda instance, x: setattr(choice, 'text', x))
    
    
    class Report(App):
        def build(self):
            Config.set('graphics', 'width', '450')
            Config.set('graphics', 'height', '250')
            return Panel()
    
    
    # Application Code
    if __name__ == '__main__':
        Report().run()
    

    输出

    【讨论】:

    • 感谢您的回复。我试过了,我仍然遇到和以前一样的问题。我还有另外两个 .py 文件,一个是 DailySummaryReport.py 和另一个 WeeklySummaryReport.py,第一个我有一个名为 DairySummary() 的函数,另一个有一个名为 WeeklySummary() 的函数。我补充说:从 DailySummaryReport 导入 DailySummary 和每周一次的相同概念,并尝试在 if elif snipped u put 以及两个 def 部分中的相应位置添加函数名称,但遗憾的是它实际上仍然没有运行任何东西。你会推荐什么?
    • 您必须执行以下操作:(1)将两个文件导入 main.py 例如导入 DailySummaryReport,导入 WeeklySummaryReport (2) 生成一个 shell 进程:os.system('python DailySummaryReport.py') 或 os.system('python WeeklySummaryReport.py')。如果您使用的是 python 3,请将其更改为 os.system('python3 DailySummaryReport.py)
    • 我已经做了导入,我做了你说的两个 + from DailySummaryReport import DailySummary & from WeeklySummaryReport import WeeklySummaryand,我试图把 os 放在最上面,但它给了我一个错误.." NameError: name 'os' is not defined" 并且它不允许我下载导入...可以跳过或以不同的方式完成吗?另外对于实际函数 DailySummary() 和 WeeklySummary(),我应该把它们放在哪里?
    • 其实我发现我已经下载了 import os 没有更多的错误显示但是它仍然无法正常工作并正确链接我的功能
    • (1) 要在生成 Python 脚本的报告中调用某些函数/方法,请使用“python-script-name.function-name”,例如WeeklySummaryReport.some_weekly_func()。 (2) 要运行整个 Python 脚本,请使用 os.system('python3 python-script.py') 例如os.system('python3 DailySummaryReport.py')。请参考更新后的示例。
    猜你喜欢
    • 1970-01-01
    • 2018-11-11
    • 2014-01-12
    • 1970-01-01
    • 2012-06-04
    • 1970-01-01
    • 1970-01-01
    • 2013-07-20
    • 1970-01-01
    相关资源
    最近更新 更多