程序问答   发布时间:2022-05-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了画布没有正确对齐大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决画布没有正确对齐?

开发过程中遇到画布没有正确对齐的问题如何解决?下面主要结合日常开发的经验,给出你关于画布没有正确对齐的解决方法建议,希望对你解决画布没有正确对齐有所启发或帮助;

我正在尝试使用 kivy 和 kivymd 制作一个聊天应用程序,但我遇到了一个问题。我试图在标签小部件之前放置一个画布,但它没有正确对齐。请帮我解决这个问题。

Kivy 代码

<ChatScreen>:
    GrIDLayout:
        cols: 1
        MDToolbar:
            title: 'My Room'
            anchor_title: 'center'
            left_action_items: [['menu',lambda x: x]]
            right_action_items: [['logout-variant',lambda x:x]]
        ScrollVIEw:
            BoxLayout:
                ID: chat_area
                orIEntation: 'vertical'
                size_hint: 1,1
                canvas.before:
                    color:
                        rgba: 0,0.2
                    Rectangle:
                        pos: self.pos
                        size: self.size
        Widget:
            ID: separator
            size_hint: 1,0.008
            pos_hint: {'center_x':0.5,'center_y':0.5}
            canvas:
                color:
                    rgba: 0,1
                Rectangle:
                    pos: 0,separator.center_y
                    size: separator.wIDth,1.5
        GrIDLayout:
            cols: 2
            size_hint: 1,0.1
            TexTinput:
                ID: msg
                hint_text: 'Enter message'
                BACkground_color: 0,0  
                foreground_color: 0,1
            MDiconbutton:
                icon: 'send'
                on_press: root.send_msg()

Python 代码

class ChatScreen(Screen):
    def send_msg(self):
        msg = self.IDs.msg.text
        if msg=='':
            pass
        else:
            label = MDLabel(
                text=msg,size_hint=(0.3,1),pos_hint={'center_x': 0.7,'center_y': 0.5}
                )
            with label.canvas.before:
                color(0,1,0.7)
                RoundedRectangle(radius=[30,30,30],size=label.size,pos=label.pos)
            self.IDs.chat_area.add_Widget(label)
            self.IDs.msg.text = ''

点击按钮时添加的标签画布相互堆叠。请帮忙。

解决方法

你的 RoundedRectangle 不在正确位置的原因是你创建 @H_169_11@mDLabel 时设置了位置和大小,所以它使用默认值 (0,0)(100,100)

您可以通过为消息定义一个类来利用 kivy 为您提供的自动绑定。也许是这样的

class Chatmessage(MDLabel):
    pass

然后在 kv 中为该类添加规则:

<Chatmessage>:
    size_hint: (0.3,1)
    pos_hint: {'center_x': 0.7,'center_y': 0.5}
    canvas.before:
        Color:
            rgba: (0,1,0.7)
        RoundedRectangle:
            radius: [30,30,30]
            pos: self.pos
            size: self.size

并在您的 send_msg() 方法中使用新类:

class ChatScreen(Screen):
    def send_msg(self):
        msg = self.ids.msg.text
        if msg=='':
            pass
        else:
            label = Chatmessage(text=msg)
            self.ids.chat_area.add_widget(label)
            self.ids.msg.text = ''

现在 RoundedRectangle 将自动调整为 Chatmessage 的大小和位置。

大佬总结

以上是大佬教程为你收集整理的画布没有正确对齐全部内容,希望文章能够帮你解决画布没有正确对齐所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: