Code: Alles auswählen
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# For Python3.x
'''
Created on Mar 21, 2016
@author: Bill Begueradj
'''
import tkinter as tk
import tkinter.ttk as ttk
Code: Alles auswählen
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# For Python3.x
'''
Created on Mar 21, 2016
@author: Bill Begueradj
'''
import tkinter as tk
import tkinter.ttk as ttk
Code: Alles auswählen
def main():
root=tk.Tk()
label = ['position', 'lieferant', 'artikel', 'benennung', 'hersteller',
'herstellernummer', 've', 'inhalt', 'vk', 'mwst']
data = {
('123', '123456789') :['', '123', '123456789',
'produkt 123456789, din a4, weiß, 80g',
'hersteller', 'herstellernummer', 'stück', '1', '3.25', '0.19']
}
width = [10, 10, 20, 50, 25, 30, 10, 10, 15, 10]
d=Begueradj(root, label, data, width)
# Define the different GUI widgets
label_0 = tk.Label(root, width=15, bg='lavender', text = 'LABEL0',
anchor=tk.E)
value_0 = tk.Entry(root)
label_0.grid(row = 2, column = 0, sticky = tk.E)
value_0.grid(row = 2, column = 1, sticky = tk.W)
label_1 = tk.Label(root, width=15, bg='lavender', text = 'LABEL1',
anchor=tk.E)
value_1 = tk.Entry(root)
label_1.grid(row = 3, column = 0, sticky = tk.E)
value_1.grid(row = 3, column = 1, sticky = tk.W)
# Set the buttons
submit_button = tk.Button(root, text = "Insert", command = d.insert_data)
submit_button.grid(row = 4, column = 1, sticky = tk.W)
exit_button = tk.Button(root, text = "Exit", command = d.output_data)
exit_button.grid(row = 4, column = 2, sticky = tk.W)
root.mainloop()
if __name__=="__main__":
main()
Code: Alles auswählen
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# For Python3.x
import tkinter as tk
import tkinter.ttk as ttk
'''
Created on Mar 21, 2016
@author: Bill Begueradj
'''
class Begueradj(tk.Frame):
'''
classdocs
'''
def __init__(self, parent):
'''
Constructor
'''
tk.Frame.__init__(self, parent)
self.parent=parent
self.initialize_user_interface()
def initialize_user_interface(self):
"""Draw a user interface allowing the user to type
items and insert them into the treeview
"""
self.parent.title("Canvas Test")
self.parent.grid_rowconfigure(0, weight=1)
self.parent.grid_columnconfigure(0, weight=1)
self.parent.config(background="lavender")
# Set the treeview
self.tree = ttk.Treeview(self.parent, selectmode="extended",
columns=self.parent.label)
# Set the scrollbar
xcroll =tk.Scrollbar(self.parent, orient=tk.HORIZONTAL,
bg='lightblue', activebackground='lightsteelblue',
troughcolor='white', command=self.tree.xview)
ycroll =tk.Scrollbar(self.parent, orient=tk.VERTICAL,
bg='lightblue', activebackground='lightsteelblue',
troughcolor='white', command=self.tree.yview)
self.tree.configure(yscrollcommand=ycroll.set,
xscrollcommand=xcroll.set)
ycroll.grid(row=0, column=4, sticky=tk.NS, in_=self.parent)
xcroll.grid(row=1, column=0, columnspan=4, sticky=tk.EW,
in_=self.parent)
self.parent.rowconfigure(0, weight=1)
self.parent.columnconfigure(0, weight=1)
# Set the binding for three
self.tree.bind("<Double-Button-1>", self.select_data)
self.tree.bind('<KP_Enter>', self.select_data)
self.tree.bind('<Return>', self.select_data)
self.treeview = self.tree
# Initialize the counter
self.i = 1
def select_data(self, event):
item = self.treeview.focus()
selection = self.treeview.item(item)["values"]
print(selection)
def main():
root=tk.Tk()
root.label = ['position', 'lieferant', 'artikel', 'benennung', 'hersteller',
'herstellernummer', 've', 'inhalt', 'vk', 'mwst']
products = {
('123', '123456789') :['', '123', '123456789',
'produkt 123456789, din a4, weiß, 80g',
'hersteller', 'herstellernummer', 'stück', '1', '3.25', '0.19']
}
width2label = [10, 10, 20, 50, 25, 30, 10, 10, 15, 10]
d=Begueradj(root)
# Set the label for table and the entry-fields
for i, name in enumerate(root.label):
name = name.upper()
text = '{}:'.format(name)
width = width2label[i] * 10
# Set the treeview
d.tree.heading('{}'.format(i), text=name, anchor=tk.W)
d.tree.column('{}'.format(i), width=width, stretch=tk.YES)
d.tree.grid(row=0, columnspan=i+1, sticky=tk.NSEW)
# Define the different GUI widgets
root.label_0 = tk.Label(root, width=15, bg='lavender', text = 'LABEL0',
anchor=tk.E)
root.value_0 = tk.Entry(root)
root.label_0.grid(row = 2, column = 0, sticky = tk.E)
root.value_0.grid(row = 2, column = 1, sticky = tk.W)
root.label_1 = tk.Label(root, width=15, bg='lavender', text = 'LABEL1',
anchor=tk.E)
root.value_1 = tk.Entry(root)
root.label_1.grid(row = 3, column = 0, sticky = tk.E)
root.value_1.grid(row = 3, column = 1, sticky = tk.W)
# Set the buttons
submit_button = tk.Button(root, text = "Insert",
command = lambda: insert_data(d, products, root.value_0, root.value_1))
submit_button.grid(row = 4, column = 1, sticky = tk.W)
exit_button = tk.Button(root, text = "Exit",
command = lambda: output_data(d))
exit_button.grid(row = 4, column = 2, sticky = tk.W)
root.mainloop()
def insert_data(d, products, value_0, value_1):
"""
Insertion method.
"""
try:
key = value_0.get(), value_1.get()
data = products[key]
except KeyError:
if set(key) == {''}:
print('Neuer Datensatz')
return
pos = '#{}{}'.format((3 - len(str(d.i))) * '0', d.i)
data[0] = str(pos)
d.treeview.insert('', 'end', text='ITEM_{}'.format(d.i),
values=[x.upper() for x in data])
# Increment counter
d.i = d.i + 1
def output_data(d):
"""
Output method.
"""
d.quit()
for child in d.treeview.get_children():
print(d.treeview.item(child)["values"])
if __name__=="__main__":
main()