如果这不是不证自明的,我只编写python 2.7 tkinter大约四个月。我明白了
from Tkinter import* #access to libraries and widgets to write python code
root=Tk() #creates CLI window that actually runs the module
root.mainloop() #repeats the module code ad nauseum
class = Class(root) #no clue what this actually does
我写了一些简单的单类模块,即
from Tkinter import*
class Code:
def __init__(self, parent):
self.myParent = parent
self.main_frame = Frame(parent, background="light blue")
self.main_frame.pack(expand=YES, fill=BOTH)
self.enter = Entry(self.main_frame, width=30)
self.enter.pack(side=LEFT, expand=NO)
root = Tk()
code = Code(root)
root.mainloop()
现在我在互联网上遇到了一个有趣的小小部件类 - 我已经删除了必需品 - 这可以使那个无聊的输入窗口成为一个自动完成的输入窗口,即
import Tkinter
class AutocompleteEntry(Tkinter.Entry):
def set_completion_list(self, completion_list):
self._completion_list = completion_list
self._hits = []
self._hit_index = 0
self.position = 0
self.bind('<KeyRelease>', self.handle_keyrelease)
def autocomplete(self, delta=0):
if delta:
self.delete(self.position, Tkinter.END)
else:
self.position = len(self.get())
_hits = []
for element in self._completion_list:
if element.startswith(self.get().lower()):
_hits.append(element)
if _hits != self._hits:
self._hit_index = 0
self._hits=_hits
if _hits == self._hits and self._hits:
self._hit_index = (self._hit_index + delta) % len(self._hits)
if self._hits:
self.delete(0,Tkinter.END)
self.insert(0,self._hits[self._hit_index])
self.select_range(self.position,Tkinter.END)
def handle_keyrelease(self, event):
if len(event.keysym)== 1:
self.autocomplete()
if __name__ == '__main__':
test_list = ('test', 'type', 'true', 'tree')
root = Tkinter.Tk()
entry = AutocompleteEntry(root)
entry.set_completion_list(test_list)
entry.pack()
entry.focus_set()
root.mainloop()
这两个都很好地独奏,但我有几个问题集成它们,其中最重要的是区分Tkinter import * / root = Tk()和import Tkinter / root = Tkinter.Tk()。我以为这只是表达同样事情的两种方式?
就代码类,widget-class组合而言,我需要哪些更大的框架来粉碎这两者?我应该创建一个基类并让另一个继承吗?我应该把它们一个堆叠在另一个上面吗?我应该把它们放在一个更大的超级班下吗?
更新的组合课程
from Tkinter import*
class AutocompleteEntry(Entry):
def set_completion_list(self, completion_list):
self._completion_list = completion_list
self._hits = []
self._hit_index = 0
self.position = 0
self.bind('<KeyRelease>', self.handle_keyrelease)
def autocomplete(self, delta=0):
if delta:
self.delete(self.position,END)
else:
self.position = len(self.get())
_hits = []
for element in self._completion_list:
if element.startswith(self.get().lower()):
_hits.append(element)
if _hits != self._hits:
self._hit_index = 0
self._hits=_hits
if _hits == self._hits and self._hits:
self._hit_index = (self._hit_index + delta) % len(self._hits)
if self._hits:
self.delete(0,END)
self.insert(0,self._hits[self._hit_index])
self.select_range(self.position,END)
def handle_keyrelease(self, event):
if len(event.keysym)== 1:
self.autocomplete()
class Code:
def __init__(self, parent):
self.myParent = parent
self.main_frame = Frame(parent, background="light blue")
self.main_frame.pack(expand=YES, fill=BOTH)
test_list = ('test', 'type', 'true', 'tree')
self.enter = AutocompleteEntry(self.main_frame, width=30)
self.enter.set_completion_list(test_list)
self.enter.pack(side=LEFT, expand=NO)
root = Tk()
code = Code(root)
root.mainloop()
答案 0 :(得分:0)
from Tkinter import *; root=Tk()
和import Tkinter; root=Tkinter.Tk()
是或多或少两种表达同一事物的方式。至少,两者都给你一个根窗口。但是,它们并不完全相同。前者将大量内容导入命名空间,后者仅导入一件事。
我在网上看到很多from Tkinter import *
的例子,但我认为这是一个坏主意。我的首选是import Tkinter as tk
,其中(a)不会污染命名空间,(b)升级到python 3时需要的变化很小,(c)在调用Tkinter函数时非常清楚,当你是不
关于你有两个根的问题......不,不要这样做。您只能在应用程序中拥有Tk类的单个实例。要一起使用它们,要么单独导入它们(即:不要将它们复制并粘贴到单个文件中),要么必须修改其中一个,以便它们使用相同的导入策略。