Как определить & ldquo; command + backspace & rdquo; с внешней клавиатуры в UITextField

Сделать это прокручиваемым

Используйте этот удобный класс, чтобы прокручивать фрейм, содержащий ваши виджеты. Выполните следующие шаги:

  1. создать кадр
  2. отобразить его (пакет, сетка и т. Д.)
  3. сделать его прокручиваемым
  4. добавить в него виджеты
  5. вызвать метод update ()

import tkinter as tk
from tkinter import ttk

class Scrollable(ttk.Frame):
    """
       Make a frame scrollable with scrollbar on the right.
       After adding or removing widgets to the scrollable frame, 
       call the update() method to refresh the scrollable area.
    """

    def __init__(self, frame, width=16):

        scrollbar = tk.Scrollbar(frame, width=width)
        scrollbar.pack(side=tk.RIGHT, fill=tk.Y, expand=False)

        self.canvas = tk.Canvas(frame, yscrollcommand=scrollbar.set)
        self.canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)

        scrollbar.config(command=self.canvas.yview)

        self.canvas.bind('<Configure>', self.__fill_canvas)

        # base class initialization
        tk.Frame.__init__(self, frame)         

        # assign this obj (the inner frame) to the windows item of the canvas
        self.windows_item = self.canvas.create_window(0,0, window=self, anchor=tk.NW)


    def __fill_canvas(self, event):
        "Enlarge the windows item to the canvas width"

        canvas_width = event.width
        self.canvas.itemconfig(self.windows_item, width = canvas_width)        

    def update(self):
        "Update the canvas and the scrollregion"

        self.update_idletasks()
        self.canvas.config(scrollregion=self.canvas.bbox(self.windows_item))

Пример использования

root = tk.Tk()

header = ttk.Frame(root)
body = ttk.Frame(root)
footer = ttk.Frame(root)
header.pack()
body.pack()
footer.pack()

ttk.Label(header, text="The header").pack()
ttk.Label(footer, text="The Footer").pack()


scrollable_body = Scrollable(body, width=32)

for i in range(30):
    ttk.Button(scrollable_body, text="I'm a button in the scrollable frame").grid()

scrollable_body.update()

root.mainloop()
0
задан rmaddy 15 January 2019 в 20:14
поделиться

1 ответ

Вы можете использовать UIKeyCommand. Я тестировал на своем 10,5 "iPad Pro под управлением iOS 12.1.1, используя клавиатуру Bluetooth и приложение Swift Playgrounds версии 2.2.

Вот код игровой площадки:

import UIKit
import PlaygroundSupport

class ViewController: UIViewController {
    override func loadView() {
        let command = UIKeyCommand(input: "\u{8}", modifierFlags: .command, action: #selector(ViewController.handleKeyCommand), discoverabilityTitle: "Hello")
        addKeyCommand(command)

        let view = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
        view.contentMode = .topLeft
        view.backgroundColor = .white

        let stack = UIStackView()
        stack.axis = .vertical
        stack.spacing = 8
        stack.alignment = .fill
        stack.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(stack)
        NSLayoutConstraint.activate([
            view.leadingAnchor.constraint(equalTo: stack.leadingAnchor),
            view.trailingAnchor.constraint(equalTo: stack.trailingAnchor),
            view.topAnchor.constraint(equalTo: stack.topAnchor)])

        let textField = UITextField(frame: CGRect(x: 20, y: 20, width: 260, height: 30))
        textField.borderStyle = .roundedRect
        textField.translatesAutoresizingMaskIntoConstraints = false
        stack.addArrangedSubview(textField)

        label.translatesAutoresizingMaskIntoConstraints = false
        stack.addArrangedSubview(label)

        self.view = view
    }

    @objc func handleKeyCommand(_ sender: UIKeyCommand) {
        commandCount += 1
        label.text = "\(commandCount)"
    }

    private var commandCount = 0
    private let label = UILabel()
}

let vc = ViewController()
PlaygroundPage.current.liveView = vc

Нажмите на текст нажмите ⌘⌫ . Каждый раз, когда вы нажимаете его, счетчик в метке увеличивается на 1.

0
ответ дан rob mayoff 15 January 2019 в 20:14
поделиться
Другие вопросы по тегам:

Похожие вопросы: