无法将命令绑定到按钮Python 3.7

时间:2019-04-23 16:09:41

标签: python python-3.x button tkinter

我试图获得一个按钮来使用Python(使用Tkinter)做一些事情。我希望该按钮获得组合框的当前选择并在屏幕上打印。

图形布局是3个单独的框架,但是按钮和组合框都位于同一框架(框架#2)中。

问题是我无法引用组合框。我正在阅读的错误:

import UIKit

class ViewController: UIViewController, UIScrollViewDelegate {
    // MARK: - Properties

    var scrollView: UIScrollView!

    let numPages = 6
    var pages = [UIView?]()

        var imageViews:[UIImageView] = [UIImageView](){
        didSet{
            print("imageViews.count: \(imageViews.count)")
        }
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        _ = setupInitialPages
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        scrollView = UIScrollView(frame: view.bounds)
        scrollView.translatesAutoresizingMaskIntoConstraints = false
        scrollView.isPagingEnabled = true
        view.addSubview(self.scrollView)
        scrollView.backgroundColor = UIColor.blue
        scrollView.delegate = self
        scrollView.indicatorStyle = .white

        NSLayoutConstraint.activate([
            scrollView.topAnchor.constraint(equalTo: self.view.topAnchor),
            scrollView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
            scrollView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
            scrollView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor)
            ])

        pages = [UIView?](repeating: nil, count: numPages)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    // MARK: - Initial Setup

    lazy var setupInitialPages: Void = {

        adjustScrollView()

        loadPage(0)
        loadPage(1)
    }()

    // MARK: - Utilities

    fileprivate func removeAnyImages() {
        for page in pages where page != nil {
            page?.removeFromSuperview()
        }
    }

    fileprivate func adjustScrollView() {
        scrollView.contentSize =
            CGSize(width: scrollView.frame.width * CGFloat(numPages),
                   height: scrollView.frame.height - topLayoutGuide.length)
    }

    func getImage(page:Int)->UIImage{
        let resourceName = String(page + 1)
        return #imageLiteral(resourceName: resourceName)
    }

    fileprivate func loadPage(_ page: Int) {
        guard page < numPages && page != -1 else { return }
        if pages[page] == nil {
                let newImageView = getRecycledImageView()
                newImageView.image = getImage(page: page)
                newImageView.contentMode = .scaleAspectFit
                newImageView.backgroundColor = UIColor.yellow

                var frame = scrollView.frame

                frame.origin.x = frame.width * CGFloat(page)

                frame.origin.y = -self.topLayoutGuide.length
                frame.size.height += self.topLayoutGuide.length
                let canvasView = UIView(frame: frame)
                scrollView.addSubview(canvasView)

                newImageView.translatesAutoresizingMaskIntoConstraints = false
                canvasView.addSubview(newImageView)

                NSLayoutConstraint.activate([
                    (newImageView.leadingAnchor.constraint(equalTo: canvasView.leadingAnchor)),
                    (newImageView.trailingAnchor.constraint(equalTo: canvasView.trailingAnchor)),
                    (newImageView.topAnchor.constraint(equalTo: canvasView.topAnchor)),
                    (newImageView.bottomAnchor.constraint(equalTo: canvasView.bottomAnchor))
                    ])

                pages[page] = canvasView
//            }
        }
    }

    func getRecycledImageView()->UIImageView{
        let unusedImageViews = imageViews.filter { (imageView) -> Bool in
            return imageView.isDescendant(of: scrollView) == false
        }
        if let unusedImageView = unusedImageViews.first{
            print("reusing imageView")
            return unusedImageView
        }else{
            let imageView = UIImageView()
            imageViews.append(imageView)
            return imageView
        }
    }

    fileprivate func loadCurrentPages(page: Int) {
        guard (page > 0 && page + 1 < numPages) || transitioning else { return }

        // Remove all of the images and start over.
        removeAnyImages()
        pages = [UIView?](repeating: nil, count: numPages)

        loadPage(Int(page) - 1)
        loadPage(Int(page))
        loadPage(Int(page) + 1)
    }

    fileprivate func gotoPage(page: Int, animated: Bool) {
        loadCurrentPages(page: page)

        var bounds = scrollView.bounds
        bounds.origin.x = bounds.width * CGFloat(page)
        bounds.origin.y = 0
        scrollView.scrollRectToVisible(bounds, animated: animated)
    }


    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let pageWidth = scrollView.frame.width
        let page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1
        loadCurrentPages(page: Int(page))
    }

}

Frame object has no attribute 'box'

Window object has no attribute 'box'

我尝试过:

self.box=ttk.Combobox(self.frame2 , values[...])
self.button1=tk.Button(self.frame2, command= self.wipe(), text=...)

def wipe(self):
    self.box.get()

目标是从组合框中简单地获取选定的选项。

这里的代码最少,会产生相同的错误:

def wipe(self):
    self.frame2.box.get()

1 个答案:

答案 0 :(得分:0)

我会在问题中添加标签“ tkinter”。

尝试以下操作:

def wipe(self):
    # code

self.box=ttk.Combobox(self.frame2 , values[...])
self.button1=tk.Button(self.frame2, command=wipe, text=...)

注意以下几点:

  1. 我先定义了擦拭巾,然后才使用它。
  2. 我不确定您为什么要command=self.wipe(),因为这里有两个问题。首先,将command设置为self.wipe()的结果,这不是函数。其次,您尚未定义self.wipe,尚未定义wipe
  3. command=wipecommand关键字参数设置为 函数wipe

我处理tkinter已经很久了,如果这不起作用,我会尝试再次检查文档来寻求帮助。

相关问题