gpt4 book ai didi

python - 在 Kivy 中创建自定义图像类,将 mag_filter 设置为 "nearest"

转载 作者:行者123 更新时间:2023-12-01 02:16:57 30 4
gpt4 key购买 nike

我在这里做错了什么?我无法让自定义图像类将 mag_filter 设置为“最近”。像素图像非常模糊,单独设置mag_filter很烦人。

这是我的Python:

#test2.py
import kivy
kivy.require("1.10.0")
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.image import Image

class Container(BoxLayout):
pass

class CustomImage(Image):
def build(self):
#self.ids.img.texture.mag_filter = 'nearest' #doesn't work
#self.img.texture.mag_filter = 'nearest' #doesn't work
self.texture.mag_filter = 'nearest' #doesn't work
#texture.mag_filter = 'nearest' #doesn't work

class TestName(App):
def build(self):
global root
Builder.load_file("picTest.kv")
root = Container()
return root

### Keep everything below this last! ###
if __name__ == '__main__':
TestName().run()

这是相应的 Kivy 文件

#picTest.kv
Container:

#Container holds all the other layouts
<Container>:
id: contain
CustomImage:
source: "smile.png"
CustomImage:
source: "smile.png"
CustomImage:
source: "smile.png"

<CustomImage>:
id: img
allow_stretch: True

“微笑.png”:

smile.png

最佳答案

您必须通过绑定(bind)纹理来完成此操作,并且在回调中您必须更改纹理,此连接必须在构造函数中完成。

#test2.py
import kivy
kivy.require("1.10.0")
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.image import Image

class Container(BoxLayout):
pass

class CustomImage(Image):
def __init__(self, *args, **kwargs):
Image.__init__(self, *args, **kwargs)
self.bind(texture=self._update_texture_filters)

def _update_texture_filters(self, image, texture):
texture.mag_filter = 'nearest'

class TestName(App):
def build(self):
Builder.load_file("picTest.kv")
root = Container()
return root

### Keep everything below this last! ###
if __name__ == '__main__':
TestName().run()

之前:

enter image description here

之后:

enter image description here

关于python - 在 Kivy 中创建自定义图像类,将 mag_filter 设置为 "nearest",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48306896/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com