Created
November 7, 2012 18:29
-
-
Save anonymous/4033430 to your computer and use it in GitHub Desktop.
Tkinter, Listbox-Auswahl mit Übergabe an Entry-Liste
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# For Python3.x | |
import os | |
import csv | |
import codecs | |
import tkinter as tk | |
CONFIG = {'width' : 0, | |
'font': ('NimbusSansL', 14), | |
'font_color': 'black', | |
'back_ground' : 'orange', | |
'select_mode': 'single'} | |
class ListGui(object): | |
def __init__(self, controller, items, items2, main_title, sub_title, | |
conf): | |
self.root = tk.Tk() | |
self.main_title = main_title | |
self.sub_title = sub_title | |
self.root.title(self.main_title) | |
self.controller = controller | |
self.conf = conf | |
xpos = 0 | |
ypos = 0 | |
self.screenx = self.root.winfo_screenwidth() | |
self.screeny = self.root.winfo_screenheight() | |
self.root.geometry("%dx%d+%d+%d" % (self.screenx, self.screeny, | |
xpos, ypos)) | |
self.frame = tk.Frame(self.root) | |
self.frame.pack(side='top', fill='both', expand=True) | |
maxline = (max([len(' '.join(item)) for item in sorted(items)])) | |
minline = (min([len(' '.join(item)) for item in sorted(items)])) | |
step = (maxline - minline) | |
if (maxline - minline) == 0: | |
step = 4 | |
for item in sorted(items): | |
if type(items[item]) == tuple: | |
self.items = ['{0}\t{1}{2}'.format(' '.join(item), | |
((maxline + step - len(' '.join(item))) * ' '), | |
' '.join(items[item])) for item in sorted(items)] | |
if type(items[item]) == str: | |
self.items = ['{0}\t{1}{2}'.format(' '.join(item), | |
((maxline + step - len(' '.join(item))) * ' '), | |
''.join(items[item])) for item in sorted(items)] | |
self.items2 = items2 | |
box_length = len(self.items) | |
max_items = (max([(len(' '.join(item)) + | |
len(' '.join(items[item]))) | |
for item in sorted(items)])) - 5 | |
if max_items > 160: | |
max_items = 160 | |
self.geoxy = set() | |
if self.sub_title != '': | |
self.label_name = tk.Label(self.frame, height=2, | |
width=self.conf['width'], text=self.sub_title, | |
bg=self.conf['back_ground'], font=self.conf['font'], bd=0, | |
highlightthickness=0, fg=self.conf['font_color']) | |
self.label_name.pack(fill='both', expand=False) | |
self.geoxy.add(self.label_name.winfo_reqheight()) | |
self.listbox = tk.Listbox(self.frame, width=max_items, | |
font=self.conf['font'], fg=self.conf['font_color'], | |
selectmode=self.conf['select_mode'], bd=0, highlightthickness=0) | |
self.listbox.pack(pady=30, expand=True) | |
self.listbox.insert(0, *self.items) | |
self.listbox.bind('<<ListboxSelect>>', self.check) | |
self.geoxy.add(self.listbox.winfo_reqheight()) | |
self.root.config() | |
label_names = self.items2 | |
self.scr_frame = self.set_scrolled_entries(self.root, | |
label_names, self.conf) | |
self.scr_frame.pack(expand=True) | |
self.button_frame = tk.Label(self.root, bg=self.conf['back_ground']) | |
self.button_frame.pack(fill='x', ipady=5) | |
tk.Button(self.button_frame, text="Abbrechen", | |
font=self.conf['font'], fg=self.conf['font_color'], | |
highlightthickness=0, command=self.root.destroy,).pack( | |
side='right', padx=4) | |
tk.Button(self.button_frame, text="Übernehmen", | |
font=self.conf['font'], fg=self.conf['font_color'], | |
highlightthickness=0, | |
command=self.controller.collect_entries).pack( | |
side='right', padx=4) | |
self.geoxy.add(self.button_frame.winfo_reqheight()) | |
def set_scrolled_entries(self, parent, label_names, conf): | |
self.conf = conf | |
self.entry_vars = [tk.StringVar() for _ in label_names] | |
self.entries = [] | |
scroll_frame = tk.Frame(parent, bg='red') | |
canvas = tk.Canvas(scroll_frame, highlightthickness=0) | |
canvas.grid(row=0, column=0, sticky=tk.NSEW) | |
yscroll = tk.Scrollbar(scroll_frame, orient=tk.VERTICAL, | |
command=canvas.yview) | |
yscroll.grid(row=0, column=1, sticky=tk.NS) | |
canvas.config(yscrollcommand=yscroll.set) | |
scroll_frame.grid_rowconfigure(0, weight=1) | |
self.canvas = canvas | |
self.bind_scroll(yscroll, self.y_scroll) | |
entry_frame = tk.Frame(canvas) | |
font = self.conf['font'] | |
for i, (var, name) in enumerate(zip(self.entry_vars, label_names)): | |
label = tk.Label(entry_frame, text=name, font=font) | |
label.grid(row=i, column=0, padx=10, sticky=tk.E) | |
bg = ('grey', 'grey')[i & 1] | |
entry = tk.Entry(entry_frame, textvariable=var, width=100, | |
font=font, bg=bg) | |
entry.grid(row=i, column=1, pady=2, sticky=tk.W) | |
self.entries.append(entry) | |
canvas.create_window(0, 0, window=entry_frame, anchor=tk.NW) | |
canvas.update_idletasks() | |
x, y, w, h = entry_frame.bbox(tk.ALL) | |
y_value = h | |
if (h * 1.5) > self.screeny: | |
y_value = (self.screeny - (round(sum(self.geoxy) * 2))) | |
canvas.config(scrollregion=(x, y, w, h), width=w, height=y_value) | |
return scroll_frame | |
def check(self, event): | |
indices = list(map(int, self.listbox.curselection())) | |
self.controller.process(self.items[indices[0]]) | |
def run(self): | |
self.root.mainloop() | |
def bind_scroll(self, widget, mode): | |
#~~ Windows | |
widget.bind("<MouseWheel>", mode) | |
#~~ Linux | |
widget.bind("<Button-4>", mode) | |
widget.bind("<Button-5>", mode) | |
def y_scroll(self, event): | |
if event.num == 5 or event.delta < 0: | |
self.canvas.yview_scroll(1, "unit") | |
elif event.num == 4 or event.delta > 0: | |
self.canvas.yview_scroll(-1, "unit") | |
class Controller(object): | |
def __init__(self, LISTBOX_ITEMS, DEFAULT_LABELS, MAIN_TITLE, SUB_TITLE): | |
self.model = Model(LISTBOX_ITEMS) | |
self.model2 = Model(DEFAULT_LABELS) | |
self.view = ListGui(self, self.model.items, self.model2.items, | |
MAIN_TITLE, SUB_TITLE, CONFIG) | |
def process(self, listbox_item): | |
self.view.entry_vars[0].set(listbox_item) | |
print(self.view.entry_vars[0]) | |
print(listbox_item) | |
def collect_entries(self): | |
entry_data = dict() | |
for index, entry_var in enumerate(self.view.entry_vars): | |
print("{} : {}".format(self.model2.items[index], | |
entry_var.get())) | |
entry_data[self.model2.items[index]] = entry_var.get() | |
print() | |
print(entry_data) | |
def run(self): | |
self.view.run() | |
class Model(object): | |
def __init__(self, items): | |
self.items = items | |
self.items2 = items | |
def main(): | |
Controller(LISTBOX_ITEMS, DEFAULT_LABELS, MAIN_TITLE, SUB_TITLE).run() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment