简体   繁体   中英

Image Buttons Kivy

Trying to call vsOpen from ImageButton inside LauncherApp();the goal was to make buttons that have images on them that respond to input. Calling VsOpen says I'm missing self as an argument if self is then placed in I receive "ImageButton has no attribute self". As you can see below I have normal buttons working however I can not seem to properly create a button image. Would someone care to explain how I would go about this?

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout 
from kivy.config import Config
from kivy.uix.image import Image
from kivy.uix.button import Button
from kivy.uix.behaviors import ButtonBehavior
from cryptography.fernet import Fernet
from colorama import Fore, Style
import webbrowser as browser
import subprocess
import os

PATHs=[#other_program_names/locations_go_here]

class ImageButton(ButtonBehavior, Image):  
            def on_press(self):  
                for x in range(0, 2):
                    iD=Fernet.generate_key()
                    cipher = Fernet(iD)
                    url = input(">")
                    crypt_url=cipher.encrypt(bytes(url, "utf-8"))
                    if url != "":
                        if url.startswith("www.", 0,4):
                            dcrypt_url=cipher.decrypt(bytes(crypt_url))
                            browser.open_new_tab(dcrypt_url)
                            print(Fore.BLUE+"Accessing " + url + "..")
                        else:
                            print("String given was not a url")
                            quit
                    else:
                        print(Fore.RED+">no url was entered..")
                        quit

            def vsOpen(self):
                try:
                    if os.path.isfile(PATHs[0]) == 1:
                        self.x=subprocess.call(PATHs[0])
                    else:
                        print(Fore.RED+"vsCode was not found"+Style.BRIGHT)
                except TypeError:
                    print("Type error has occured at vsOpen")
                    quit


        class LauncherApp(App):
            def build(self):
                layout=FloatLayout(size=(200,200))
                #Normal Buttons were here
                layout.add_widget(ImageButton.vsOpen())
                layout.add_widget(ImageButton.on_press())
                return layout

root = LauncherApp()
if __name__ == "__main__":
    root.run()
layout.add_widget(ImageButton.vsOpen())

I'm not sure what you expect this to do, but it makes no sense. vsOpen is a method of the ImageButton class, you should only call it from a class instance, not the class itself (unless you know what you are doing and have a really good reason).

Even if this ran the method somehow, it doesn't return a widget that you could add to the layout, so it's unclear what you actually want to achieve.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM