Python 如何在tkinter中对齐两个帧的行?

Python 如何在tkinter中对齐两个帧的行?,python,tkinter,tkinter-canvas,tkinter-layout,Python,Tkinter,Tkinter Canvas,Tkinter Layout,我有两个镜头在特金特。其中一个具有标签,而另一个具有文本字段和按钮。当用户单击按钮时,在第二帧中水平添加更多字段。两个框架都连接到一个垂直滚动条,第二个框架有自己的水平滚动条。如何使第一帧的第一行与第二帧的第一行对齐 我希望字段与A、B、C标签对齐 from tkinter import * import pyodbc # Importing for storing in a DB from tkinter import messagebox import getpass import os

我有两个镜头在特金特。其中一个具有标签,而另一个具有文本字段和按钮。当用户单击按钮时,在第二帧中水平添加更多字段。两个框架都连接到一个垂直滚动条,第二个框架有自己的水平滚动条。如何使第一帧的第一行与第二帧的第一行对齐

我希望字段与A、B、C标签对齐

from tkinter import *
import pyodbc  # Importing for storing in a DB
from tkinter import messagebox
import getpass
import os
from datetime import datetime
from tkinter import ttk
import win32com.client
import openpyxl

from openpyxl import load_workbook
from tkcalendar import Calendar, DateEntry

global totalItems
global itemNum
global sizeList
global typeList
global qtyList
global actualQtyList

actualQtyList=[]

itemNum=0

def myfunction(event):
    #canvas.configure(scrollregion=canvas.bbox("all"),height=650)
    canvas.configure(scrollregion=canvas.bbox("all"), height=630)
def myfunction2(event):
    canvasForCustomers.configure(scrollregion=canvasForCustomers.bbox("all"),height=645,width=900)
def multiple_yview(*args):
    canvas.yview(*args)
    canvasForCustomers.yview(*args)


def addNewCustomer():
    global totalItems
    global itemNum
    padxValue=0
    lbFrame_Customer=ttk.LabelFrame(frameForOfferCreationFilter,text="Customer")
    cb_customer=ttk.Combobox(lbFrame_Customer,state='readonly',values=customerList,width=6)
    cb_customer.grid(row=0,column=0)

    lbFrame_ShipDate=ttk.LabelFrame(frameForOfferCreationFilter,text="Ship Date")
    cal_ShipDate=DateEntry(lbFrame_ShipDate)
    cal_ShipDate.grid(row=0,column=0)

    lbFrame_PackedDateRange=ttk.LabelFrame(frameForOfferCreationFilter,text="Packed Date Range")
    cal_FromDate=DateEntry(lbFrame_PackedDateRange)
    lb_hyphen=ttk.Label(lbFrame_PackedDateRange,text="-")
    cal_ToDate = DateEntry(lbFrame_PackedDateRange)
    cal_FromDate.grid(row=0,column=0)
    lb_hyphen.grid(row=0,column=1)
    cal_ToDate.grid(row=0,column=2)

    lbFrame_Customer.grid(row=0,column=itemNum,columnspan=1,padx=padxValue)
    lbFrame_ShipDate.grid(row=0,column=itemNum+1,columnspan=1,padx=padxValue)
    lbFrame_PackedDateRange.grid(row=0,column=itemNum+2,columnspan=2,padx=padxValue)

    lb_CaseSize = ttk.Label(frameForOfferCreationFilter, text="Case Size", justify="center").grid(row=1, column=itemNum,padx=padxValue)

    if itemNum%2!=0:
        padxValue=20
    else:
        padxValue=0

    indexNum=0
    if indexNum%2==0:
        padyValue2=0
    else:
        padyValue2=5
    # frameForOfferCreation=Frame(canvasForCustomers,bg='yellow')
    # frameForOfferCreation.grid(row=2,column=0,columnspan=6)
    for indexNum in range(totalItems):
        txt_CaseSize = ttk.Entry(frameForOfferCreationItems, width=7)
        txt_CaseSize.grid(row=indexNum, column=itemNum, padx=10+padxValue,pady=padyValue2,sticky=S)

        if indexNum % 2 == 0:
            padyValue2 = 4
        else:
            padyValue2 = 0

    itemNum = itemNum + 7




root_1 = Tk()
w, h = root_1.winfo_screenwidth(), root_1.winfo_screenheight()
root_1.geometry("%dx%d+0+0" % (w, h))
root_1.title("Sales Allocation Application")
allItems=['A','B','C']
############################ Getting Customers

allCustomers=['X','Y','Z']


customerList=[]
for item in allCustomers:
    customerList.append(item[0])


tab_parent = ttk.Notebook(root_1)
tab_parent.pack(expand=1, fill='both')

tab_OfferPage = ttk.Frame(tab_parent)
tab_EditPage=ttk.Frame(tab_parent)
# tab_AllOffers = ttk.Frame(tab_parent)

tab_parent.add(tab_OfferPage, text='Create New Offer')
tab_parent.add(tab_EditPage,text="Edit Existing Offers")

lbFrm_ExistingItems=ttk.LabelFrame(tab_OfferPage,text="Items")
lbFrm_ExistingItems.pack(expand=1,fill='both')
canvas = Canvas(lbFrm_ExistingItems,width=300,height=650)  # ,bg="#F4F3F1") width 400
canvas.grid(row=0, column=0,sticky=N)
frameOfItems = Frame(canvas,bg='green')
scrollBar = ttk.Scrollbar(lbFrm_ExistingItems, orient="vertical", command=multiple_yview)  # canvas.yview)
canvas.configure(yscrollcommand=scrollBar.set)
frameOfItems.bind("<Configure>", myfunction)
scrollBar.grid(row=0, column=0, sticky=N + S + W)  # , sticky=N + S + W)
canvas.create_window((0, 0), window=frameOfItems, anchor='nw')

canvasForCustomers=Canvas(lbFrm_ExistingItems,bg="red",width=900)
canvasForCustomers.grid(row=0, column=1,sticky=N+S)
frameForOfferCreationItems=Frame(canvasForCustomers,bg='pink')
frameForOfferCreationItems.bind("<Configure>", myfunction2)
frameForOfferCreationFilter=Frame(canvasForCustomers,bg='yellow')
frameForOfferCreationFilter.bind("<Configure>", myfunction2)
canvasForCustomers.create_window((0,0),window=frameForOfferCreationFilter,anchor='nw')
canvasForCustomers.create_window((0,80),window=frameForOfferCreationItems,anchor='nw')


btn_addCustomer=ttk.Button(lbFrm_ExistingItems,text="Add\nCustomer",command=addNewCustomer).grid(row=0,column=2,sticky=N+E)
btn_loadExisting=ttk.Button(lbFrm_ExistingItems,text="Load Existing\nOpen Offers").grid(row=0,column=3,sticky=N+E)

totalItems=len(allItems)

scrollBarHorizontal = ttk.Scrollbar(lbFrm_ExistingItems, orient="horizontal", command=canvasForCustomers.xview)
canvasForCustomers.configure(xscrollcommand=scrollBarHorizontal.set)
scrollBarHorizontal.grid(row=1, column=1, sticky=E + W+S)
canvasForCustomers.configure(yscrollcommand=scrollBar.set)

qtyList=[]
for item in allItems:
    qtyList.append(Label(frameOfItems,text=item[0]))
    actualQtyList.append(item[0])


lb_Qty=ttk.Label(frameOfItems,text="Quantity").grid(row=0,column=1,pady=10)

lb_LineBreak=ttk.Label(frameOfItems,text="  ").grid(row=1,column=0,columnspan=4,pady=12)
##### Adding blank label to attempt alignement
rowNum=2
padyValue=7

for item in range(len(qtyList)):
    qtyList[item].grid(row=rowNum,column=4,pady=padyValue)

    rowNum=rowNum+1
    if rowNum%2==0:
        padyValue=7
    else:
        padyValue=0



root_1.mainloop()

从tkinter导入*
导入pyodbc#导入以存储在数据库中
从tkinter导入消息框
导入getpass
导入操作系统
从日期时间导入日期时间
从tkinter导入ttk
导入win32com.client
导入openpyxl
从openpyxl导入加载工作簿
从tkcalendar导入日历,日期输入
全球总计项目
全局itemNum
全球规模师
全局类型列表
全局qtyList
全局实际质量列表
实际QTYList=[]
itemNum=0
def myfunction(事件):
#configure(scrollregion=canvas.bbox(“全部”),高度=650)
configure(scrollregion=canvas.bbox(“全部”),高度=630)
def myfunction2(事件):
配置(scrollregion=canvasForCustomers.bbox(“全部”),高度=645,宽度=900)
def多视图(*args):
canvas.yview(*args)
canvasForCustomers.yview(*args)
def addNewCustomer():
全球总计项目
全局itemNum
padxValue=0
lbFrame_Customer=ttk.LabelFrame(frameForOfferCreationFilter,text=“Customer”)
cb_customer=ttk.Combobox(lbFrame_customer,state='readonly',value=customerList,width=6)
cb_客户网格(行=0,列=0)
lbFrame\u ShipDate=ttk.LabelFrame(frameForOfferCreationFilter,text=“发货日期”)
cal_ShipDate=日期输入(lbFrame_ShipDate)
cal_ShipDate.grid(行=0,列=0)
lbFrame_PackedDateRange=ttk.LabelFrame(frameForOfferCreationFilter,text=“打包日期范围”)
cal_FromDate=日期输入(lbFrame_PackedDateRange)
lb_hyphen=ttk.Label(lbFrame_PackedDateRange,text=“-”)
cal_ToDate=日期输入(lbFrame_PackedDateRange)
cal_FromDate.grid(行=0,列=0)
lb_连字符网格(行=0,列=1)
cal_ToDate.grid(行=0,列=2)
lbFrame_Customer.grid(行=0,列=itemNum,列span=1,padx=padxValue)
lbFrame_ShipDate.grid(行=0,列=itemNum+1,列span=1,padx=padxValue)
lbFrame_PackedDateRange.grid(行=0,列=itemNum+2,列span=2,padx=padxValue)
lb_CaseSize=ttk.Label(frameForOfferCreationFilter,text=“Case Size”,justify=“center”).grid(行=1,列=itemNum,padx=padxValue)
如果itemNum为%2=0:
padxValue=20
其他:
padxValue=0
indexNum=0
如果indexNum%2==0:
padyValue2=0
其他:
PadyValue 2=5
#frameForOfferCreation=Frame(canvasForCustomers,bg='yellow')
#frameForOfferCreation.grid(行=2,列=0,列跨度=6)
对于范围内的indexNum(totalItems):
txt\u CaseSize=ttk.Entry(frameForOfferCreationItems,宽度=7)
txt_CaseSize.grid(行=indexNum,列=itemNum,padx=10+padxValue,pady=padyValue2,sticky=S)
如果indexNum%2==0:
PadyValue 2=4
其他:
padyValue2=0
itemNum=itemNum+7
根_1=Tk()
w、 h=root_1.winfo_screenwidth(),root_1.winfo_screenheight()
根1.几何体(“%dx%d+0+0”%(w,h))
root_1.标题(“销售分配应用程序”)
allItems=['A','B','C']
############################吸引顾客
所有客户=['X','Y','Z']
customerList=[]
对于所有客户中的项目:
customerList.append(项[0])
tab\u parent=ttk.Notebook(根目录1)
tab_parent.pack(expand=1,fill='both')
tab\u OfferPage=ttk.Frame(tab\u父项)
tab\u EditPage=ttk.Frame(tab\u父项)
#tab\u AllOffers=ttk.Frame(tab\u父项)
tab_parent.add(tab_OfferPage,text='Create New Offer')
tab_parent.add(tab_EditPage,text=“编辑现有报价”)
lbFrm_ExistingItems=ttk.LabelFrame(标签页,text=“Items”)
lbFrm_ExistingItems.pack(expand=1,fill='both')
画布=画布(lbFrm_现有项目,宽度=300,高度=650)宽度400
网格(行=0,列=0,粘性=N)
frameOfItems=Frame(画布,背景为绿色)
scrollBar=ttk.scrollBar(lbFrm_ExistingItems,orient=“vertical”,command=multiple_yview)#canvas.yview)
configure(yscrollcommand=scrollBar.set)
frameOfItems.bind(“,myfunction)
滚动条.grid(行=0,列=0,粘性=N+S+W)#,粘性=N+S+W)
canvas.create_window((0,0),window=frameOfItems,anchor='nw')
canvasForCustomers=Canvas(lbFrm_现有项目,bg=“红色”,宽度=900)
canvasForCustomers.grid(行=0,列=1,粘性=N+S)
frameForOfferCreationItems=Frame(canvasForCustomers,bg='pink')
frameForOfferCreationItems.bind(“,myfunction2)
frameForOfferCreationFilter=Frame(canvasForCustomers,bg='yellow')
frameForOfferCreationFilter.bind(“,myfunction2)
canvasForCustomers.create_window((0,0),window=frameForOfferCreationFilter,anchor='nw')
canvasForCustomers.create_window((0,80),window=frameForOfferCreationItems,anchor='nw')
btn\u addCustomer=ttk.按钮(lbFrm\u ExistingItems,text=“Add\nCustomer”,command=addNewCustomer).网格(行=0,列=2,粘性=N+E)
btn_loadExisting=ttk.按钮(lbFrm_ExistingItems,text=“loadExisting\nOpen Offers”).grid(行=0,列=3,粘性=N+E)
totalItems=len(所有项目)
scrollBarHorizontal=ttk.Scrollbar(lbFrm_ExistingItems,orient=“horizontal”,command=canvasForCustomers.xview)
canvasForCustomers.configure(xscrollcommand=scrollBarHorizontal.set)
scrollBarHorizontal.grid(行=1,列=1,粘滞=E+W+S)
canvasForCustomers.configure(yscrollcommand=scrollBar.set)
qtyList=[]
对于allItems中的项目:
append(标签(frameOfItems,text=item[0]))
actualQtyList.append(项[0])
lb_Qty=ttk.Label(frameOfItems,text=“Quantity”).grid(行=0,列=1,pady=10)
lb_LineBreak=ttk.Label(frameOfItems,text=”“).grid(行=1,列=0,列span=4,pady=12)
#####添加空白标签以尝试对齐
rowNum=2
padyValue=7
对于范围(len(qtyList))中的项目:
qtyList[item].grid(行=rowNum,列=4,pady=padyValue)
rowNum=rowNum+1
如果rowNum%2==0:
padyValue=7
其他:
padyValue=0
root_1.mainloop()