无法增加 tkinter 框架 canvas.create_window 的大小,隐藏 CSV 列
Posted
技术标签:
【中文标题】无法增加 tkinter 框架 canvas.create_window 的大小,隐藏 CSV 列【英文标题】:Unable to increase size of tkinter frame canvas.create_window, Hides CSV columns 【发布时间】:2020-12-04 14:08:41 【问题描述】:我的员工 CSV 文件包含以下列 姓名,年龄,学历,mailid,手机号码,地址1,地址2,地址3,地址4 使用 Canvas Frame,create_window(4,4) 这是我下面的代码 我尝试增加宽度和高度
但直到 address2 列仅可见,最后两列 Address3,Address4 在屏幕中不可见 有人知道吗?请推荐
员工.csv
name,age,education,mailid,mobilenumber,Address1,Address2,Address3,Address4
abc,23,bsc,abc@gmail.com,9999999999,addr1,addr2,addr3,addr4
abcdf,26,bsc,abcdf@gmail.com,88888888888,addr1,addr2,addr3,addr4
abc,23,bsc,abc@gmail.com,9999999999,addr1,addr2,addr3,addr4
abcdf,26,bsc,abcdf@gmail.com,88888888888,addr1,addr2,addr3,addr4
abc,23,bsc,abc@gmail.com,9999999999,addr1,addr2,addr3,addr4
abcdf,26,bsc,abcdf@gmail.com,88888888888,addr1,addr2,addr3,addr4
abc,23,bsc,abc@gmail.com,9999999999,addr1,addr2,addr3,addr4
abcdf,26,bsc,abcdf@gmail.com,88888888888,addr1,addr2,addr3,addr4
abc,23,bsc,abc@gmail.com,9999999999,addr1,addr2,addr3,addr4
abcdf,26,bsc,abcdf@gmail.com,88888888888,addr1,addr2,addr3,addr4
import csv
import os
import codecs
from tkinter import *
from tkinter import Tk
from tkinter import ttk
import tkinter as tk
import tkinter.ttk
from tkinter import filedialog
class MyApp():
def __init__(self):
UI_Widgets(self)
def myfunction(event):
canvas.configure(scrollregion=canvas.bbox("all"),width=450,height=300)
root=Tk()
SIZEX = 800
SIZEY = 600
POSX = 100
POSY = 100
root.title("MY APPLICATION")
root.configure(background="#e7e6e6")
root.wm_geometry("%dx%d+%d+%d" % (SIZEX, SIZEY, POSX, POSY))
myframe=Frame(root,relief=GROOVE,width=450,height=400,bd=2,background="white",bg="black")
myframe.place(x=50,y=100)
canvas=Canvas(myframe)
scrollable_frame=Frame(canvas,bg ="#f4f6f9")
myscrollbar=Scrollbar(myframe,orient="vertical",command=canvas.yview)
canvas.configure(yscrollcommand=myscrollbar.set,bg='#f4f6f9')
myscrollbar.pack(side="right",fill="y")
canvas.pack(side="left")
canvas.create_window((4,4),window=scrollable_frame,anchor='nw' )
scrollable_frame.bind("<Configure>",myfunction)
sep = ttk.Separator(scrollable_frame,orient=VERTICAL)
def UI_Widgets(self):
self.Button_readcsv= Button(root,text ="Browse CSV FILE", command = readcsv)
self.Button_readcsv.place(x=40, y=40)
def readcsv():
filepath=filedialog.askopenfilename(initialdir="/", title="Open file",filetypes=(("CSV Files","*.csv"),))
try:
with codecs .open(filepath, encoding="utf8") as my_file:
reader = csv.DictReader(my_file, delimiter=',')
headers = reader.fieldnames
i=0
horline=0
tk.Label(scrollable_frame, text=headers[0] ,bg ="#f4f6f9" ).grid(row=0,column=1,sticky='n')
tkinter.ttk.Separator(scrollable_frame, orient=VERTICAL).grid(column=2, row=0, rowspan=3, sticky='ns')
tk.Label(scrollable_frame, text=headers[1],bg ="#f4f6f9").grid(row=0,column=3,sticky='n')
tkinter.ttk.Separator(scrollable_frame, orient=VERTICAL).grid(column=4, row=0, rowspan=3, sticky='ns')
tk.Label(scrollable_frame, text=headers[2],bg ="#f4f6f9").grid(row=0,column=5,sticky='n')
tkinter.ttk.Separator(scrollable_frame, orient=VERTICAL).grid(column=6, row=0, rowspan=3, sticky='ns')
tk.Label(scrollable_frame, text=headers[3],bg ="#f4f6f9").grid(row=0,column=7,sticky='n')
tkinter.ttk.Separator(scrollable_frame, orient=VERTICAL).grid(column=8, row=0, rowspan=3, sticky='ns')
tk.Label(scrollable_frame, text=headers[4],bg ="#f4f6f9").grid(row=0,column=9,sticky='n')
tkinter.ttk.Separator(scrollable_frame, orient=VERTICAL).grid(column=10, row=0, rowspan=3, sticky='ns')
tk.Label(scrollable_frame, text=headers[5],bg ="#f4f6f9").grid(row=0,column=11,sticky='n')
tkinter.ttk.Separator(scrollable_frame, orient=VERTICAL).grid(column=12, row=0, rowspan=3, sticky='ns')
tk.Label(scrollable_frame, text=headers[6],bg ="#f4f6f9").grid(row=0,column=13,sticky='n')
tkinter.ttk.Separator(scrollable_frame, orient=VERTICAL).grid(column=14, row=0, rowspan=3, sticky='ns')
tk.Label(scrollable_frame, text=headers[7],bg ="#f4f6f9").grid(row=0,column=15,sticky='n')
tkinter.ttk.Separator(scrollable_frame, orient=VERTICAL).grid(column=16, row=0, rowspan=3, sticky='ns')
tk.Label(scrollable_frame, text=headers[8],bg ="#f4f6f9").grid(row=0,column=17,sticky='n')
tkinter.ttk.Separator(scrollable_frame, orient=VERTICAL).grid(column=18, row=0, rowspan=3, sticky='ns')
for row in reader:
if(i==0):
i=3
else:
i=i+2
if(horline==0):
horline=2
else:
horline=horline+2
name = row[headers[0]]
age = row[headers[1]]
education = row[headers[2]]
mailid = row[headers[3]]
mobilenumber = row[headers[4]]
Address1 = row[headers[5]]
Address2 = row[headers[6]]
Address3 = row[headers[7]]
Address4 = row[headers[8]]
tk.Label(scrollable_frame,text=name,bg ="#f4f6f9").grid(row=i,column=1,sticky='n')
tkinter.ttk.Separator(scrollable_frame, orient=VERTICAL).grid(column=2, row=0, rowspan=i+1, sticky='ns')
tk.Label(scrollable_frame, text=age,bg ="#f4f6f9").grid(row=i,column=3,sticky='n')
tkinter.ttk.Separator(scrollable_frame, orient=VERTICAL).grid(column=4, row=0, rowspan=i+1, sticky='ns')
tk.Label(scrollable_frame, text=education,bg ="#f4f6f9").grid(row=i,column=5,sticky='n')
tkinter.ttk.Separator(scrollable_frame, orient=VERTICAL).grid(column=6, row=0, rowspan=i+1, sticky='ns')
tk.Label(scrollable_frame, text=mailid,bg ="#f4f6f9").grid(row=i,column=7,sticky='n')
tkinter.ttk.Separator(scrollable_frame, orient=VERTICAL).grid(column=8, row=0, rowspan=i+1, sticky='ns')
tk.Label(scrollable_frame, text=mobilenumber,bg ="#f4f6f9").grid(row=i,column=9,sticky='n')
tkinter.ttk.Separator(scrollable_frame, orient=VERTICAL).grid(column=10, row=0, rowspan=i+1, sticky='ns')
tk.Label(scrollable_frame, text=Address1,bg ="#f4f6f9").grid(row=i,column=11,sticky='n')
tkinter.ttk.Separator(scrollable_frame, orient=VERTICAL).grid(column=12, row=0, rowspan=i+1, sticky='ns')
tk.Label(scrollable_frame, text=Address2,bg ="#f4f6f9").grid(row=i,column=13,sticky='n')
tkinter.ttk.Separator(scrollable_frame, orient=VERTICAL).grid(column=14, row=0, rowspan=i+1, sticky='ns')
tk.Label(scrollable_frame, text=Address3,bg ="#f4f6f9").grid(row=i,column=15,sticky='n')
tkinter.ttk.Separator(scrollable_frame, orient=VERTICAL).grid(column=16, row=0, rowspan=i+1, sticky='ns')
tk.Label(scrollable_frame, text=Address4,bg ="#f4f6f9").grid(row=i,column=17,sticky='n')
tkinter.ttk.Separator(scrollable_frame, orient=VERTICAL).grid(column=18, row=0, rowspan=i+1, sticky='ns')
except(IOError):
print("Error Reading in CSV File ")
if __name__ == '__main__':
myapp = MyApp()
root.mainloop()
【问题讨论】:
【参考方案1】:可能随着宽度和高度的改变不起作用,因为您正在更改框架大小,并且框架将根据其中的画布大小进行调整,除非您使用 pack_propagate(False)
阻止它。但这对您没有帮助,因为您必须在 my_function()
方法中编辑画布宽度:
def myfunction(event):
canvas.configure(scrollregion=canvas.bbox("all"),width=450,height=300)
但要确保它始终适合您的画布,最好添加一个水平滚动条:
myscrollbarx=Scrollbar(myframe,orient="horizontal",command=canvas.xview)
canvas.configure(xscrollcommand=myscrollbarx.set,bg='#f4f6f9')
myscrollbarx.pack(side="bottom",fill="x")
【讨论】:
不客气,欢迎来到 ***以上是关于无法增加 tkinter 框架 canvas.create_window 的大小,隐藏 CSV 列的主要内容,如果未能解决你的问题,请参考以下文章
我已经将tkinter窗口划分为单元格(使用框架)。如何定义适用于每个单元格的方法?