我无法获得一个条目值并使其成为我的其余代码的可用变量,这部分只能创建一个变量z,但它最终为空,这是我尝试不同的方法没有好结果。
entry.get()
什么都不返回,我需要它返回一个字符串。
from Tkinter import *
class Application(Frame):
def __init__(self, master=None):
Frame.__init__(self,master)
self.grid()
self.initialize()
def initialize(self):
self.btn1 = Button(self, text = 'Ok', command=retrieve_input)
self.btn1.grid()
def retrieve_input():
print 'Input value => %s' %entry.get()
root = Tk()
root.title('Teste')
root.geometry('200x100')
entry = Entry(root)
entry.grid(column=0,row=0)
entry.focus()
entry.bind('<Return>', (lambda event: retrieve_input()))
z=entry.get()
app = Application(root)
root.mainloop()
答案 0 :(得分:0)
您需要致电:
z = entry.get()
每当希望z
成为entry
中的内容时 。与替换retrieve_input
时一样:
def retrieve_input():
global z
z = entry.get()
print 'Input value => %s' %z
例如。
答案 1 :(得分:0)
如果您想添加一个名称与try: # In order to be able to import tkinter for
import tkinter as tk # either in python 2 or in python 3
except ImportError:
import Tkinter as tk
def add_variable():
global variables, entry
variables[entry.get()] = None
print(variables)
if __name__ == '__main__':
root = tk.Tk()
variables = dict()
entry = tk.Entry(root)
button = tk.Button(root, text="Add variable...", command=add_variable)
entry.pack()
button.pack()
root.mainloop()
中所写内容一样的变量,您可以填写import UIKit
class ViewController: UIViewController, UISearchControllerDelegate {
var controladorDeBusca: UISearchController!
var resultsTableViewController: ResultsTableViewController?
override func viewDidLoad() {
super.viewDidLoad()
resultsTableViewController = storyboard!.instantiateViewController(withIdentifier: "resultsTableViewController") as? ResultsTableViewController
configurarControladorDeBusca()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func configurarControladorDeBusca() {
controladorDeBusca = UISearchController(searchResultsController: resultsTableViewController)
controladorDeBusca.delegate = self
controladorDeBusca.searchResultsUpdater = resultsTableViewController
controladorDeBusca.dimsBackgroundDuringPresentation = true
definesPresentationContext = true
controladorDeBusca.loadViewIfNeeded()
//Configura a barra do Controlador de busca
controladorDeBusca.searchBar.delegate = resultsTableViewController
controladorDeBusca.hidesNavigationBarDuringPresentation = false
controladorDeBusca.searchBar.placeholder = "Search place"
controladorDeBusca.searchBar.sizeToFit()
controladorDeBusca.searchBar.barTintColor = navigationController?.navigationBar.barTintColor
controladorDeBusca.searchBar.tintColor = self.view.tintColor
//Adiciona a barra do Controlador de Busca a barra do navegador
navigationItem.titleView = controladorDeBusca.searchBar
}
}
字典,如下面的代码所示:
import UIKit
class ResultsTableViewController: UITableViewController, UISearchResultsUpdating, UISearchBarDelegate {
var array = ["Brazil", "Bolivia", "United States", "Canada", "England", "Germany", "France", "Portugal"]
var arrayFilter = [String]()
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return arrayFilter.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "searchCell", for: indexPath)
cell.textLabel?.text = arrayFilter[indexPath.row]
return cell
}
func updateSearchResults(for searchController: UISearchController) {
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
arrayFilter.removeAll()
if let text = searchBar.text {
for string in array {
if string.contains(text) {
arrayFilter.append(string)
}
}
}
tableView.reloadData()
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
arrayFilter.removeAll()
tableView.reloadData()
}
func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
arrayFilter.removeAll()
tableView.reloadData()
return true
}
}