Python tkinter使用anchor = W的文本显示GUI

时间:2017-03-29 05:50:56

标签: python tkinter

我正在尝试使用anchor = W将所有文本对齐,但无法完成。

GUI脚本

我想用数字显示所有单行,例如: 我也想在开始时显示枚举,尝试它不起作用。

1.U1a1400     3C      CAN Initialisation is Failure - no sub type information
2.U000188     3C      High Speed CAN Communication Bus - bus off

代码如下:

import pypyodbc
from Tkinter import *
from DataStructure import *
row = ''
dtc_code = ''

def UserInput():
global UI_MainForm
global row
global dtc_code
UI_MainForm = Tk()

#Initialization of Main Window  


UI_MainForm.geometry("1000x575+500+50")
UI_MainForm.title("Script")

# labelframe = LabelFrame(UI_MainForm,text="Please select :",width=400, height=800,bd = 2)
# labelframe.pack(fill="both")
# labelframe.config(relief=RIDGE)
# for xx,yy in zip(DTC_CODE,FAULT_TYPES):
    # temp_text1 = '{0} - {1}'.format(xx,yy)

    for dtccodes,faulttypes in zip(DTC_CODE,FAULT_TYPES):
            temp_text = '{0}      {1}'.format(dtccodes,faulttypes)

        Label(UI_MainForm, text= temp_text).pack(anchor = W)


    for menu1,menu2 in zip(DTC_Description,DTC_Description1):
            temp_text = '{0} - {1}'.format(menu1,menu2)
        Label(UI_MainForm, text= temp_text).pack(anchor = W)

    # for menu2 in DTC_Description1:
        # Label(labelframe, text= menu2).pack(anchor = W,pady = 2)





# Label(labelframe, text=array2).pack(anchor = E,pady = 2)

UI_MainForm.mainloop()
return; 


response = '59 02 FF DA 14 00 3C C0 01 88 3C 5B 18 2F 3C C3 00 00 3C E1 00 00 3C E1 01 00 3C C3 00 57 3C E0 1A 54 3C 50 24 00 3C'

DTCLogged = response.split(' ')

DTCLogged = DTCLogged[3:]
print DTCLogged
DTCLoggedLen = len(DTCLogged)
print DTCLoggedLen
NumberOfDTC = DTCLoggedLen/4
print NumberOfDTC
loopindex = 0
DTCRecord =[]
DTC_CODE = []
FAULT_TYPES = []
DTC_Description = []
DTC_Description1 = []
while (loopindex+4) <= DTCLoggedLen:


Record = DTCLogged[loopindex]+' '+DTCLogged[loopindex+1]+' '+DTCLogged[loopindex+2]
# print Record
#print DTCLogged[loopindex]
DTCRecord.append(Record)




if((int(DTCLogged[loopindex], 16) & 0xC0) == 0):
  dtc_designator = 'P'
  #print dtc_designator
elif((int(DTCLogged[loopindex], 16) & 0xC0) == 64):
   dtc_designator = 'C'
   #print dtc_designator
elif((int(DTCLogged[loopindex], 16) & 0xC0) == 128):
   dtc_designator = 'B'
   #print dtc_designator
else:
   dtc_designator = 'U';
   #print dtc_designator

#y = int(DTCLogged[loopindex], 16) & 0x30
global dtc_designator1
if((int(DTCLogged[loopindex], 16) & 0x30) == 0):
  dtc_designator1 = '0'
  #print dtc_designator1
elif((int(DTCLogged[loopindex], 16) & 0x30) == 16):
   dtc_designator1 = '1'
   #print dtc_designator1
elif((int(DTCLogged[loopindex], 16) & 0x30) == 32):
   dtc_designator1 = '2'
   #print dtc_designator1
else:
   dtc_designator = '3';
   #print dtc_designator1

z = int(DTCLogged[loopindex], 16) & 0x0F
global dtc_designator2
dtc_designator2 = hex(z)[2:]
#print dtc_designator2

dtc_code = dtc_designator + dtc_designator1 + dtc_designator2 + DTCLogged[loopindex+1]
# print dtc_code

dtc_code1 = dtc_code + DTCLogged[loopindex+2]
# Value = []
DTC_CODE.append(dtc_code1)
# return Value

fault_type = DTCLogged[loopindex+3]
FAULT_TYPES.append(fault_type)

连接到MS ACCESS:

cursor1.execute("SELECT Field2 FROM DTC_CODES Where Field1 = '{}'".format(dtc_code))

for row in cursor1.fetchone():
   print row
DTC_Description.append(row)

cursor2 = connection.cursor()
cursor2.execute("SELECT Field2 FROM FAULT_TYPES Where Field1 = '{}'".format(DTCLogged[loopindex+2]))
for row1 in cursor2.fetchone():
    DTC_Description1.append(row1)

loopindex = loopindex+4


UserInput()

1 个答案:

答案 0 :(得分:1)

anchor=W指定标签内的文字出现在哪一侧。它对标签相对于其他标签的位置没有影响。

除非您另行指定,否则pack会使用side='top'选项。因此,Label(UI_MainForm, text= temp_text).pack(anchor = W)会将此标签放在窗口中的任何其他位置。

如果您希望所有内容都在水平线上,请使用side='left'

Label(UI_MainForm, text= temp_text).pack(side='left', anchor = W)