ModuleNotFoundError: No module named 'ui'

ModuleNotFoundError: No module named 'ui'

我正在尝试 运行 我的 mac 书中 IDLE 中的 pythonista 绘图应用程序示例中的以下脚本,但收到错误消息“ModuleNotFoundError:没有名为 'ui' 的模块“

UI 本身的网站:https://omz-software.com/pythonista/docs/ios/ui.html

我似乎找不到此 ui 的任何安装程序,非常感谢任何帮助,因为它 运行 在我的 ipad 上,所以我想看看它是如何安装的运行 也在我的 mac 上。

import ui
import photos
import console

# The PathView class is responsible for tracking
# touches and drawing the current stroke.
# It is used by SketchView.

class PathView (ui.View):
    def __init__(self, frame):
        self.frame = frame
        self.flex = 'WH'
        self.path = None
        self.action = None
    
    def touch_began(self, touch):
        x, y = touch.location
        self.path = ui.Path()
        self.path.line_width = 8.0
        self.path.line_join_style = ui.LINE_JOIN_ROUND
        self.path.line_cap_style = ui.LINE_CAP_ROUND
        self.path.move_to(x, y)
    
    def touch_moved(self, touch):
        x, y = touch.location
        self.path.line_to(x, y)
        self.set_needs_display()
    
    def touch_ended(self, touch):
        # Send the current path to the SketchView:
        if callable(self.action):
            self.action(self)
        # Clear the view (the path has now been rendered
        # into the SketchView's image view):
        self.path = None
        self.set_needs_display()
    
    def draw(self):
        if self.path:
            self.path.stroke()

# The main SketchView contains a PathView for the current
# line and an ImageView for rendering completed strokes.
# It also manages the 'Clear' and 'Save' ButtonItems that
# are shown in the title bar.

class SketchView (ui.View):
    def __init__(self, width=1024, height=1024):
        self.bg_color = 'white'
        iv = ui.ImageView(frame=(0, 0, width, height))
        pv = PathView(frame=self.bounds)
        pv.action = self.path_action
        self.add_subview(iv)
        self.add_subview(pv)
        save_button = ui.ButtonItem()
        save_button.title = 'Save Image'
        save_button.action = self.save_action
        clear_button = ui.ButtonItem()
        clear_button.title = 'Clear'
        clear_button.tint_color = 'red'
        clear_button.action = self.clear_action
        self.right_button_items = [save_button, clear_button]
        self.image_view = iv
    
    def path_action(self, sender):
        path = sender.path
        old_img = self.image_view.image
        width, height = self.image_view.width, self.image_view.height
        with ui.ImageContext(width, height) as ctx:
            if old_img:
                old_img.draw()
            path.stroke()
            self.image_view.image = ctx.get_image()
    
    def clear_action(self, sender):
        self.image_view.image = None
    
    def save_action(self, sender):
        if self.image_view.image:
            # We draw a new image here, so that it has the current
            # orientation (the canvas is quadratic).
            with ui.ImageContext(self.width, self.height) as ctx:
                self.image_view.image.draw()
                img = ctx.get_image()
                photos.save_image(img)
                console.hud_alert('Saved')
        else:
            console.hud_alert('No Image', 'error')
    
# We use a square canvas, so that the same image
# can be used in portrait and landscape orientation.
w, h = ui.get_screen_size()
canvas_size = max(w, h)

sv = SketchView(canvas_size, canvas_size)
sv.name = 'Sketch'
sv.present('fullscreen')

你说 IDLE,Pythonista 没有,所以我假设你是在 Pythonista 之外尝试这个。 ui 模块是 Pythonista 的专有部分,不能单独安装。

如果你想在 Mac 上使用 UIKit 类 和 Python,你要么需要一本新的 M1 Macbook 可以 运行 Pythonista 应用程序与您 运行 在 iPad 上使用的应用程序相同,或者您必须查看类似 Rubicon 的内容以从 Python 桥接到 UIKit 类,本质上是复制 Pythonista ui 模块功能。