我是Python的新手,因此也是Tkinter的新手。对于第一个小项目,我正在尝试创建一个程序来编写CSV文件。程序在启动时获取文件名,检查文件中的标题并动态创建必要数量的输入字段。
为了便于继续,它显示输入的最后一行。
目前我已经过了一半,显示了最后一行和正确数量的条目小部件。不幸的是布局很垃圾。
我希望标签位于条目小部件的前面,而底部的退出按钮。
有什么想法吗?
提前致谢。
# -*- coding: utf-8 -*-
import Tkinter as tk
import argparse
import time
parser = argparse.ArgumentParser()
parser.add_argument('filename', nargs=1, help="file to parse")
args = parser.parse_args()
filename = args.filename[0]
def get_headers(filename):
with open(filename, 'r') as f:
headers = f.readline()
return(headers)
def file_len(filename):
with open(filename) as f:
for i, l in enumerate(f):
pass
return i, l
class Application(tk.Frame):
def __init__(self, headers, filename, last_line, master=None):
tk.Frame.__init__(self, master)
self.grid()
self.filename = filename
lastentry = self.createWidgets(self.filename, headers, last_line)
# print lastentry
# print(lastentry[0].get()) # Test
# lastentry[0].delete(0,tk.END) # Test
# lastentry[0].insert(0,"Test") # Test
def createWidgets(self, filename, headers, last_line):
headers = get_headers(filename).split(',') # .decode('utf8')
self.lastentry = []
self.entryfield = []
self.label = tk.Label(self, text="CSV Entry Tool").grid(columnspan=3, pady=(0,10))
for x, y in enumerate(headers):
self.create_label_widget(x, y)
for x, y in enumerate(last_line):
self.lastentry.append(self.create_lastentry_widget(x, y))
for x in range(len(last_line)):
self.entryfield.append(self.create_entry_widget(x))
self.lastlineLabel = tk.Label(self, text="Last Entry: ").grid(row=2, column=1, padx=(10,10), pady=(0,15))
self.entryLabel = tk.Label(self, text="New Entry: ").grid(row=3, column=1, padx=(10,10), pady=(0,15))
self.quitButton = tk.Button(self, text='Quit', command=self.quit)
self.quitButton.grid(row=6, column=6)
return self.lastentry
def create_label_widget(self, x, y):
new_widget = tk.Label(self.master, text=y).grid(row=1, column=x+1, padx=(10,10), pady=(0,15))
return new_widget
def create_entry_widget(self, x):
new_widget = tk.Entry(self.master)
new_widget.grid(row=3, column=x+1, padx=(10,10), pady=(0,15))
return new_widget
def create_lastentry_widget(self, x, y):
new_widget = tk.Entry(self.master)
new_widget.grid(row=2, column=x+1, padx=(10,10), pady=(0,15))
new_widget.insert(1, y)
new_widget.configure(state="readonly")
return new_widget
headers = get_headers(filename).split(',') # .decode('utf8')
print headers
# Check Filelengh and get back last line.
file_lengh, last_line = file_len(filename)
print("Einträge: ".decode('utf8')) + str(file_lengh)
last_line = last_line.split(',')
print last_line
app = Application(headers, filename, last_line)
app.master.title('Sample application')
app.mainloop()
答案 0 :(得分:2)
您可以在self
中创建标签和按钮,但在self.master
中创建其他元素 - 因此它们可以位于具有自己网格的不同小部件中。
将self.master
更改为self
并将column=x+1
更改为column=x+2
后我
我还将var = Widget(...).grid(...)
更改为var = Widget(...)
和var.grid(...)
您应该使用csv
模块来读取数据,因为正确的csv行有3列
"a","b","(x,y)"
你读为4列。
您可以在\n
"a","b","(x
y)"
您的代码将其读取为2行,但它是正确的一行。
代码:
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import Tkinter as tk
import argparse
import time
# --- classes ---
class Application(tk.Frame):
def __init__(self, headers, filename, last_line, master=None):
tk.Frame.__init__(self, master)
self.grid()
self.filename = filename
lastentry = self.createWidgets(self.filename, headers, last_line)
# print lastentry
# print(lastentry[0].get()) # Test
# lastentry[0].delete(0,tk.END) # Test
# lastentry[0].insert(0,"Test") # Test
def createWidgets(self, filename, headers, last_line):
headers = get_headers(filename).split(',') # .decode('utf8')
self.lastentry = []
self.entryfield = []
self.label = tk.Label(self, text="CSV Entry Tool")
self.label.grid(columnspan=3, pady=(0,10))
for x, y in enumerate(headers):
self.create_label_widget(x, y)
for x, y in enumerate(last_line):
self.lastentry.append(self.create_lastentry_widget(x, y))
for x in range(len(last_line)):
self.entryfield.append(self.create_entry_widget(x))
self.lastlineLabel = tk.Label(self, text="Last Entry: ")
self.lastlineLabel.grid(row=2, column=1, padx=(10,10), pady=(0,15))
self.entryLabel = tk.Label(self, text="New Entry: ")
self.entryLabel.grid(row=3, column=1, padx=(10,10), pady=(0,15))
self.quitButton = tk.Button(self, text='Quit', command=self.quit)
self.quitButton.grid(row=6, column=4)
return self.lastentry
def create_label_widget(self, x, y):
new_widget = tk.Label(self, text=y)
new_widget.grid(row=1, column=x+2, padx=(10,10), pady=(0,15))
return new_widget
def create_entry_widget(self, x):
new_widget = tk.Entry(self)
new_widget.grid(row=3, column=x+2, padx=(10,10), pady=(0,15))
return new_widget
def create_lastentry_widget(self, x, y):
new_widget = tk.Entry(self)
new_widget.grid(row=2, column=x+2, padx=(10,10), pady=(0,15))
new_widget.insert(1, y)
new_widget.configure(state="readonly")
return new_widget
# --- functions ---
def get_headers(filename):
with open(filename, 'r') as f:
headers = f.readline()
return(headers)
def file_len(filename):
with open(filename) as f:
for i, l in enumerate(f):
pass
return i, l
# --- main ---
parser = argparse.ArgumentParser()
parser.add_argument('filename', nargs=1, help="file to parse")
args = parser.parse_args()
filename = args.filename[0]
headers = get_headers(filename).split(',') # .decode('utf8')
print headers
# Check Filelengh and get back last line.
file_lengh, last_line = file_len(filename)
print("Eintrage: ".decode('utf8')) + str(file_lengh)
last_line = last_line.split(',')
print last_line
app = Application(headers, filename, last_line)
app.master.title('Sample application')
app.mainloop()