Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Kivy:使散布与子标签或图像大小相同_Python_Text_Kivy_Scatter - Fatal编程技术网

Python Kivy:使散布与子标签或图像大小相同

Python Kivy:使散布与子标签或图像大小相同,python,text,kivy,scatter,Python,Text,Kivy,Scatter,我想用kivy做一个基本的照片编辑器。它应该允许放置和转换基础图像以及添加文本标签。我使用散点实现此功能,但遇到了一个问题,即无法使根散点与其子标签或图像的大小匹配。这使得实际上很难拖动和缩放图像和标签,因为它们周围都有不可见的散射空间,因此几乎不可能拖动和调整所需的散射大小 以下是我的python代码: from kivy.app import App from kivy.uix.scatter import Scatter from kivy.uix.button import Button

我想用kivy做一个基本的照片编辑器。它应该允许放置和转换基础图像以及添加文本标签。我使用散点实现此功能,但遇到了一个问题,即无法使根散点与其子标签或图像的大小匹配。这使得实际上很难拖动和缩放图像和标签,因为它们周围都有不可见的散射空间,因此几乎不可能拖动和调整所需的散射大小

以下是我的python代码:

from kivy.app import App
from kivy.uix.scatter import Scatter
from kivy.uix.button import Button
from kivy.uix.popup import Popup
from kivy.uix.scatterlayout import ScatterLayout
from kivy.uix.label import Label
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.textinput import TextInput
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.colorpicker import ColorPicker
from kivy.logger import Logger
from kivy.graphics import Color, Ellipse, Line
from kivy.properties import ListProperty
from kivy.properties import NumericProperty
from kivy.properties import ObjectProperty
from kivy.core.window import Window
import os
#from editor import PhotoEditor
import random

#Each class def is a different screen.
class WelcomeScreen(Screen):
    pass

class PhoneSelection(Screen):
    pass

class StyleSelection(Screen):
    pass

class ColorSelection(Screen):
    pass

class DesignSelection(Screen):
    pass

class UploadPhoto(Screen):
    pass

class PatternSelection(Screen):
    pass

class PhotoEditor(Screen):
    color = ListProperty([1,1,1,1])
    word = 'Enter Text'
    font_size = 14
    font_type = ''
    image_path = 'Mike1.png'
    erase = False
    def open_popup(self):
        popup = ColorPopup()
        popup.open()
    def close_popup(self):
        color_bttn = self.ids['color_bttn']
        color_bttn.background_color = self.color
        color_bttn.color = [1 - self.color[0], 1 - self.color[1], 1 - self.color[2], 1]
    def open_text_entry(self):
        al = AnchorLayout(anchor_x='center',anchor_y='top')
        popup = TextLabelEntry()
        al.add_widget(popup)
        popup.open()
    def close_text_entry(self):
        l = Label(text=self.word,color=self.color,font_size=40)
        t = TextLabel()
        t.add_widget(l)
        t.size = l.size
        self.ids['il'].add_widget(t)

class Confirm(Screen):
    pass

class Pay(Screen):
    pass

class ThankYou(Screen):
    pass

#These classes are used as parts of the UI for other screens.
class ColorPopup(Popup):
    pass
class TextLabel(Scatter):
    pass
class TextLabelEntry(Popup):
    fonts = []
    def font_selector(self):
        for file in os.listdir("/mydir"):
            if file.endswith(".tff"):
                fonts.append(file)

class ImageLabel(ScatterLayout):
    pass
class Variables():
    #used for storing info about the case to be printed
    phone_type = ""
    phone_color = ""
    phone_style = ""
    phone_design_option = ""
    total_price = 0.0

#This is the main class that has all the app info.
class MainApp(App):
    #Window.size = (1920, 1080)
    #Window.fullscreen = True
    #list of names for each screen. Each name is used as an ID when changing the current screen.
    screens = ["welcome","choose_phone","choose_style","choose_color","choose_design","upload_photo","choose_pattern","photo_edit","confirm","pay","thanks"]
    vars = Variables()
    #The manager that holds all the screens and allows transitioning between them.
    SM = ScreenManager()
    def build(self):
        #list of names for each screen. Each name is used as an ID when changing the current screen.
        screens = ["welcome","choose_phone","choose_style","choose_color","choose_design","upload_photo","choose_pattern","photo_edit","confirm","pay","thanks"]

        #Add all screens to the manager and assign the corresponding ID.
        self.SM.add_widget(WelcomeScreen(name=screens[0]))
        self.SM.add_widget(PhoneSelection(name=screens[1]))
        self.SM.add_widget(StyleSelection(name=screens[2]))
        self.SM.add_widget(ColorSelection(name=screens[3]))
        self.SM.add_widget(DesignSelection(name=screens[4]))
        self.SM.add_widget(UploadPhoto(name=screens[5]))
        self.SM.add_widget(PatternSelection(name=screens[6]))
        self.SM.add_widget(PhotoEditor(name=screens[7]))
        self.SM.add_widget(Confirm(name=screens[8]))
        self.SM.add_widget(Pay(name=screens[9]))
        self.SM.add_widget(ThankYou(name=screens[10]))

        #Set the current screen to the welcome screen.
        self.SM.current = screens[0]
        return self.SM

#Runs the app.
if __name__ == "__main__":
    t = MainApp();
    t.run()
大多数操作发生在PhotoEditor类中

这是我的kivy代码:

<WelcomeScreen>:
    AnchorLayout:
        anchor_x: 'center'
        anchor_y: 'bottom'
        Button:
            text: "Welcome!"
            size_hint: 0.1,0.1
            on_press: root.manager.current = app.screens[7] 

<PhoneSelection>:
    BoxLayout:
        Button:
            text: "iPhone 4/4s"
            on_press: 
                root.manager.current = app.screens[1] 
                app.vars.phone_type = "iPhone YAYAYA"
                self.text = str(app.vars.phone_type)

<PhotoEditor>
    id: PhotoEditor
    FloatLayout:
        id: il
        ImageLabel:
            auto_bring_to_front: False
            scale_min: 100/img.width
            scale_max: 5000/img.width
            id: ilr
            Image:
                id: img
                source: root.image_path
                #width: il.width
                #height: il.height/self.image_ratio
                on_touch_up:
                    if int(ilr.rotation % 15) < 8: ilr.rotation = int(ilr.rotation - int(ilr.rotation % 15))
                    if int(ilr.rotation % 15) >= 8: ilr.rotation = int(ilr.rotation - int(ilr.rotation % 15)) + 15
    Image:
        id: ovrly
        source: 'overlay/iphone6.png'
    AnchorLayout:
        anchor_x: 'left'
        anchor_y: 'center'
        BoxLayout:
            size_hint: 0.1, 0.5
            orientation: 'vertical'
            Button:
                text: 'Color'
                color: [1 - root.color[0], 1 - root.color[1], 1 - root.color[2], 1]
                background_color: root.color
                background_normal: ''
                id: color_bttn
                on_press: root.open_popup()
            Button:
                group: 'text'
                text: 'Text'
                on_press: root.open_text_entry()

<ColorPopup>
    size_hint: .5, .5
    auto_dismiss: False
    title: 'Hello world'
    on_open: 
        r.value = int(r.max * app.SM.get_screen('photo_edit').color[0])
        g.value = int(g.max * app.SM.get_screen('photo_edit').color[1])
        b.value = int(b.max * app.SM.get_screen('photo_edit').color[2])
    AnchorLayout:
        anchor_x: 'center'
        anchor_y: 'center'
        AnchorLayout:
            anchor_x: 'center'
            anchor_y: 'center'
            BoxLayout:
                orientation: 'vertical'
                Slider:
                    id: r
                    on_value: 
                        app.SM.get_screen('photo_edit').color[0] = self.value_normalized
                        bttn.background_color = app.SM.get_screen('photo_edit').color
                        bttn.color = [1 - app.SM.get_screen('photo_edit').color[0], 1 - app.SM.get_screen('photo_edit').color[1], 1 - app.SM.get_screen('photo_edit').color[2], 1]
                Slider:
                    id: g
                    on_value: 
                        app.SM.get_screen('photo_edit').color[1] = self.value_normalized
                        bttn.background_color = app.SM.get_screen('photo_edit').color
                        bttn.color = [1 - app.SM.get_screen('photo_edit').color[0], 1 - app.SM.get_screen('photo_edit').color[1], 1 - app.SM.get_screen('photo_edit').color[2], 1]
                Slider:
                    id: b
                    on_value: 
                        app.SM.get_screen('photo_edit').color[2] = self.value_normalized
                        bttn.background_color = app.SM.get_screen('photo_edit').color
                        bttn.color = [1 - app.SM.get_screen('photo_edit').color[0], 1 - app.SM.get_screen('photo_edit').color[1], 1 - app.SM.get_screen('photo_edit').color[2], 1]
                Button:
                    text: 'Close'
                    id: bttn
                    background_normal: ''
                    on_press:
                        app.SM.get_screen('photo_edit').close_popup()
                        root.dismiss()


<ImageLabel>

<TextLabel>
    size: lbl.size
    on_touch_down: if app.SM.get_screen('photo_edit').erase == True: app.SM.get_screen('photo_edit').remove_widget(self)
    Label:
        id: lbl
        #on_size: root.size = self.size

<TextLabelEntry>
    size_hint: 0.5,0.5
    title: 'Text Entry'
    auto_dismiss: True
    on_open: 
        app.SM.get_screen('photo_edit').word = 'Enter Text'
    BoxLayout:
        size_hint: 0.5,0.2
        orientation: 'horizontal'
        TextInput:
            id: txt
            text: 'Enter Text'
            on_text: app.SM.get_screen('photo_edit').word = self.text
        Button:
            on_press:
                app.SM.get_screen('photo_edit').close_text_entry()
                root.dismiss()
:
主持人安排:
主播:中锋
主播:“底部”
按钮:
文字:“欢迎!”
大小提示:0.1,0.1
按:root.manager.current=app.screens[7]
:
盒子布局:
按钮:
文字:“iPhone4/4s”
新闻界:
root.manager.current=应用程序屏幕[1]
app.vars.phone_type=“iPhone YAYAYA”
self.text=str(app.vars.phone_类型)
id:PhotoEditor
浮动布局:
id:il
图像标签:
自动\u将\u带到\u前面:False
最小刻度:100/img.宽度
最大刻度:5000/img.宽度
id:ilr
图片:
id:img
来源:root.image\u路径
#宽度:il.width
#高度:il.height/self.image_比率
润色时:
如果int(ilr.rotation%15)<8:ilr.rotation=int(ilr.rotation-int(ilr.rotation%15))
如果int(ilr.rotation%15)>=8:ilr.rotation=int(ilr.rotation-int(ilr.rotation%15))+15
图片:
身份证号码:奥弗利
来源:“overlay/iphone6.png”
主持人安排:
主播:左
主播:“中心”
盒子布局:
尺寸提示:0.1,0.5
方向:“垂直”
按钮:
文字:“颜色”
颜色:[1-根.color[0],1-根.color[1],1-根.color[2],1]
背景颜色:root.color
背景\u正常:“”
id:color_bttn
按:root.open\u popup()
按钮:
组:“文本”
文本:“文本”
按:root.open\u text\u entry()
大小提示:.5,.5
自动解除:错误
标题:“你好,世界”
开放日:
r、 value=int(r.max*app.SM.get\u屏幕('photo\u edit')。颜色[0])
g、 value=int(g.max*app.SM.get\u屏幕('photo\u edit')。颜色[1])
b、 value=int(b.max*app.SM.get\u屏幕('photo\u edit')。颜色[2])
主持人安排:
主播:中锋
主播:“中心”
主持人安排:
主播:中锋
主播:“中心”
盒子布局:
方向:“垂直”
滑块:
id:r
关于价值:
app.SM.get_屏幕('photo_edit')。颜色[0]=self.value_
bttn.background\u color=app.SM.get\u屏幕('photo\u edit')。颜色
bttn.color=[1-app.SM.get_screen('photo_edit')。color[0],1-app.SM.get_screen('photo_edit')。color[1],1-app.SM.get_screen('photo_edit')。color[2],1]
滑块:
id:g
关于价值:
app.SM.get_screen('photo_edit')。color[1]=self.value_
bttn.background\u color=app.SM.get\u屏幕('photo\u edit')。颜色
bttn.color=[1-app.SM.get_screen('photo_edit')。color[0],1-app.SM.get_screen('photo_edit')。color[1],1-app.SM.get_screen('photo_edit')。color[2],1]
滑块:
身份证号码:b
关于价值:
app.SM.get_屏幕('photo_edit')。颜色[2]=self.value_
bttn.background\u color=app.SM.get\u屏幕('photo\u edit')。颜色
bttn.color=[1-app.SM.get_screen('photo_edit')。color[0],1-app.SM.get_screen('photo_edit')。color[1],1-app.SM.get_screen('photo_edit')。color[2],1]
按钮:
文本:“关闭”
id:bttn
背景\u正常:“”
新闻界:
app.SM.get_屏幕('photo_edit')。关闭_弹出窗口()
根目录
尺码:lbl.size
在触摸屏上:if app.SM.get\u screen('photo\u edit')。erase==True:app.SM.get\u screen('photo\u edit')。删除小部件(self)
标签:
id:lbl
#on_size:root.size=self.size
尺寸提示:0.5,0.5
标题:“文本输入”
自动排除:正确
开放日:
app.SM.get_screen('photo_edit')。word='Enter Text'
盒子布局:
尺寸提示:0.5,0.2
方向:“水平”
文本输入:
id:txt
文本:“输入文本”
在文本上:app.SM.get屏幕('photo\u edit')。word=self.text
按钮:
新闻界:
app.SM.get_screen('photo_edit')。关闭_text_entry()
根目录

这对我来说是一个问题已经有一段时间了,无论我做了什么尝试,我都无法修复它。任何帮助都将不胜感激。

一切都在您的标题中

散点的大小必须与图像的大小相同。 这意味着是图像定义了散射的大小

ScatterLayout:
    size: img.size
    size_hint: (None,None)
    Image:
        id: img
        size: (500, 500/self.image_ratio)    
        # Here I fix the width to 500p, 
        # You could fix the height or make them
        # related to the size of your screen.
你知道在kivy安装文件夹中有你可以做什么的例子吗?您尤其应该对kivy34\examples\demo\pictures感兴趣

比尔


PS:就我而言,我必须删除你的带有“Ovry”id的图像才能看到一些东西

我在视频中谈到了这个问题,也许这会有所帮助。