Kivy屏幕过渡

发布于 2025-02-13 23:23:53 字数 2784 浏览 3 评论 0原文

这对我来说很难解释。

我正在研究一个自动生成电子邮件签名的项目。我制作了一个预先建造的“测试”版本来尝试自己。

它的目的是您放入电子邮件地址,并在您点击“生成签名”按钮后生成电子邮件签名。

问题是,当我在放置电子邮件地址后单击“生成签名”按钮时,它无济于事。我想要的是使用电子邮件签名进入下一个屏幕。

这是我在main.py文件中拥有的内容:

from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.core.window import Window
from kivy.uix.button import Button
from kivy.app import App
from kivy.uix.label import Label
from signature import *


class EmailSignatureGenerator(App):
    """Main app class"""

    def build(self):
        self.title = "Email Signature Generator"
        return EmailScreen()


class SignatureScreen(GridLayout):
    """This is the screen that shows the signature. This also copies the signature to 
the clipboard."""

    def __init__(self, signature, **kwargs):
        super().__init__(**kwargs)
        self.cols = 1
        Window.size = (500, 300)
        self.add_widget(Label(text=str(signature)))
        self.add_widget(Label(text="Your signature has been copied to your clipboard"))
        self.add_widget(Label(text="Just go to Settings > Mail > Signature and paste!"))


class EmailScreen(GridLayout):
    """First screen that the user will see. It asks for the email address. The email 
    address is used to search for the user """

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.cols = 1
        Window.size = (500, 300)
        self.add_widget(Label(text="Email Address"))
        self.email = TextInput(multiline=False)
        self.email.focus = True
        self.add_widget(self.email)

        self.generate_signature = Button(text="Generate Signature")
        self.generate_signature.bind(on_press=SignatureScreen)
        self.add_widget(self.generate_signature)


if __name__ == "__main__":
    EmailSignatureGenerator().run()

这是签名

from main import EmailScreen


class User:
    """External or internal employee"""

    def __init__(self, full_name, job_title, phone, email):
        self.full_name = full_name
        self.job_title = job_title
        self.phone = phone
        self.email = email


class Office:
    """Office location of the corporate user"""

    def __init__(self, name, address):
        self.name = name
        self.address = address


user = User("Matt Jarrett", "Deskside Technician", "(248)XXX-XXXX", EmailScreen().email)
office = Office("Main Corporate Office", "123 Main St. New York, NY 10000")
signature = f"{user.full_name} | {user.job_title} | {office.name}" \
            f"{office.address} | {user.phone} | {user.email}"

This is kinda hard for me to explain.

I'm working on a project that automatically generates an email signature. I made a pre-built "test" version to try out myself.

The point of it is that you put in the email address and it generates an email signature after you hit the "Generate Signature" button.

Screenshot Here

The issue is that when I click the "Generate Signature" button after putting in the email address, it does nothing. What I want it to do is go to the next screen with the email signature.

Here is what I have in the main.py file:

from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.core.window import Window
from kivy.uix.button import Button
from kivy.app import App
from kivy.uix.label import Label
from signature import *


class EmailSignatureGenerator(App):
    """Main app class"""

    def build(self):
        self.title = "Email Signature Generator"
        return EmailScreen()


class SignatureScreen(GridLayout):
    """This is the screen that shows the signature. This also copies the signature to 
the clipboard."""

    def __init__(self, signature, **kwargs):
        super().__init__(**kwargs)
        self.cols = 1
        Window.size = (500, 300)
        self.add_widget(Label(text=str(signature)))
        self.add_widget(Label(text="Your signature has been copied to your clipboard"))
        self.add_widget(Label(text="Just go to Settings > Mail > Signature and paste!"))


class EmailScreen(GridLayout):
    """First screen that the user will see. It asks for the email address. The email 
    address is used to search for the user """

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.cols = 1
        Window.size = (500, 300)
        self.add_widget(Label(text="Email Address"))
        self.email = TextInput(multiline=False)
        self.email.focus = True
        self.add_widget(self.email)

        self.generate_signature = Button(text="Generate Signature")
        self.generate_signature.bind(on_press=SignatureScreen)
        self.add_widget(self.generate_signature)


if __name__ == "__main__":
    EmailSignatureGenerator().run()

And here is the signature.py

from main import EmailScreen


class User:
    """External or internal employee"""

    def __init__(self, full_name, job_title, phone, email):
        self.full_name = full_name
        self.job_title = job_title
        self.phone = phone
        self.email = email


class Office:
    """Office location of the corporate user"""

    def __init__(self, name, address):
        self.name = name
        self.address = address


user = User("Matt Jarrett", "Deskside Technician", "(248)XXX-XXXX", EmailScreen().email)
office = Office("Main Corporate Office", "123 Main St. New York, NY 10000")
signature = f"{user.full_name} | {user.job_title} | {office.name}" \
            f"{office.address} | {user.phone} | {user.email}"

Thank you, any advice is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

划一舟意中人 2025-02-20 23:23:54

如评论中所述,您可以使用 screenmanager 。您要做的就是制作emailscreensignaturesCreen从Kivy screen继承,然后将它们添加到您的应用程序中的ScreenManager中:

from kivy.uix.gridlayout import GridLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.textinput import TextInput
from kivy.core.window import Window
from kivy.uix.button import Button
from kivy.app import App
from kivy.uix.label import Label


class EmailSignatureGenerator(App):
    """Main app class"""
    def build(self):
        self.title = "Email Signature Generator"
        self.sm = ScreenManager()
        emailscreen = EmailScreen(name='email')
        signaturescreen = SignatureScreen(name='signature')
        self.sm.add_widget(emailscreen)
        self.sm.add_widget(signaturescreen)

        return self.sm


class SignatureScreen(Screen):
    """This is the screen that shows the signature. This also copies the signature to 
the clipboard."""

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.container = GridLayout()
        self.add_widget(self.container)
        self.container.cols = 1
        Window.size = (500, 300)
        self.container.add_widget(Label(text="Your signature has been copied to your clipboard"))
        self.container.add_widget(Label(text="Just go to Settings > Mail > Signature and paste!"))


class EmailScreen(Screen):
    """First screen that the user will see. It asks for the email address. The email 
    address is used to search for the user """

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.container = GridLayout()
        self.add_widget(self.container)
        self.container.cols = 1
        Window.size = (500, 300)
        self.container.add_widget(Label(text="Email Address"))
        self.email = TextInput(multiline=False)
        self.email.focus = True
        self.container.add_widget(self.email)

        self.generate_signature = Button(text="Generate Signature")
        self.generate_signature.bind(on_press=self.switch_screens)
        self.container.add_widget(self.generate_signature)

    def switch_screens(self, _event):
        self.parent.current = 'signature'


class User:
    """External or internal employee"""

    def __init__(self, full_name, job_title, phone, email):
        self.full_name = full_name
        self.job_title = job_title
        self.phone = phone
        self.email = email


class Office:
    """Office location of the corporate user"""

    def __init__(self, name, address):
        self.name = name
        self.address = address


user = User("Matt Jarrett", "Deskside Technician", "(248)XXX-XXXX", EmailScreen().email)
office = Office("Main Corporate Office", "123 Main St. New York, NY 10000")
signature = f"{user.full_name} | {user.job_title} | {office.name}" \
            f"{office.address} | {user.phone} | {user.email}"

if __name__ == "__main__":
    EmailSignatureGenerator().run()

这只是一个如何切换屏幕的示例。您可以将代码的任何其他功能添加到其中。

As said in the comments, you can use ScreenManager. All you have to do is make EmailScreen and SignatureScreen inherit from a kivy Screen and add them into a ScreenManager in your app:

from kivy.uix.gridlayout import GridLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.textinput import TextInput
from kivy.core.window import Window
from kivy.uix.button import Button
from kivy.app import App
from kivy.uix.label import Label


class EmailSignatureGenerator(App):
    """Main app class"""
    def build(self):
        self.title = "Email Signature Generator"
        self.sm = ScreenManager()
        emailscreen = EmailScreen(name='email')
        signaturescreen = SignatureScreen(name='signature')
        self.sm.add_widget(emailscreen)
        self.sm.add_widget(signaturescreen)

        return self.sm


class SignatureScreen(Screen):
    """This is the screen that shows the signature. This also copies the signature to 
the clipboard."""

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.container = GridLayout()
        self.add_widget(self.container)
        self.container.cols = 1
        Window.size = (500, 300)
        self.container.add_widget(Label(text="Your signature has been copied to your clipboard"))
        self.container.add_widget(Label(text="Just go to Settings > Mail > Signature and paste!"))


class EmailScreen(Screen):
    """First screen that the user will see. It asks for the email address. The email 
    address is used to search for the user """

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.container = GridLayout()
        self.add_widget(self.container)
        self.container.cols = 1
        Window.size = (500, 300)
        self.container.add_widget(Label(text="Email Address"))
        self.email = TextInput(multiline=False)
        self.email.focus = True
        self.container.add_widget(self.email)

        self.generate_signature = Button(text="Generate Signature")
        self.generate_signature.bind(on_press=self.switch_screens)
        self.container.add_widget(self.generate_signature)

    def switch_screens(self, _event):
        self.parent.current = 'signature'


class User:
    """External or internal employee"""

    def __init__(self, full_name, job_title, phone, email):
        self.full_name = full_name
        self.job_title = job_title
        self.phone = phone
        self.email = email


class Office:
    """Office location of the corporate user"""

    def __init__(self, name, address):
        self.name = name
        self.address = address


user = User("Matt Jarrett", "Deskside Technician", "(248)XXX-XXXX", EmailScreen().email)
office = Office("Main Corporate Office", "123 Main St. New York, NY 10000")
signature = f"{user.full_name} | {user.job_title} | {office.name}" \
            f"{office.address} | {user.phone} | {user.email}"

if __name__ == "__main__":
    EmailSignatureGenerator().run()

This is just an example of how to switch screens. You can add any other features of your code to it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文