Tkinter对齐文本

时间:2018-11-19 15:40:03

标签: python tkinter

我正在使用Tkinter来对齐屏幕文本。我未能正确对齐要显示的文本。目前,这是在24英寸显示器上显示的,我无法部分显示文本一直到屏幕右侧。我已经尝试过

sticky=E 

但是它没有将文本向右移动。Screen Image

from __future__ import division
import matplotlib
matplotlib.use('Agg')
from Tkinter import *
import tkFont
import RPi.GPIO as GPIO
import time
import datetime

win = Tk()

###################################################################################################
##################################### CONFIGURATIONS ##############################################
###################################################################################################


#Change according to the true target value for this shift
TARGET_VALUE = 600

#Change according to the target value displayed for this shift
TARGET_DISPLAY = 480

#Change to met you display sizes , try by increasing and decreasing this value to change labels
SIZE = 120


###################################################################################################
########################################### END ###################################################
###################################################################################################


GPIO.setmode(GPIO.BOARD)
GPIO.setup(40, GPIO.IN,pull_up_down=GPIO.PUD_DOWN)

ACTUAL_VALUE = IntVar()
ACTUAL_VALUE.set(0)

ACTUAL_CTR=0

Efficiency_VALUE = IntVar()
Efficiency_VALUE.set(100)

StartTime = datetime.datetime.now()
currentState  = 0
previousState = 0

myFont = tkFont.Font(family = 'Helvetica', size = SIZE , weight = 'bold')
win.attributes("-fullscreen",True)
BACKGROUND = 'white'
win.configure(background=BACKGROUND)

def getNumberOfMinutes(a,b):
        c=b-a
        x=divmod(c.days*86400 + c.seconds,60)
        return x[0]

def RESET():
    global StartTime
    global Efficiency_VALUE
    global ACTUAL_VALUE
    global BACKGROUND
    global ACTUAL_CTR

    print(" Reset pressed")
    ACTUAL_VALUE.set(0)
    ACTUAL_CTR=0
    Efficiency_VALUE.set(100)
    StartTime = datetime.datetime.now()
    BACKGROUND = 'white'
    win.configure(background=BACKGROUND)

def task():
    global StartTime
    global TARGET_VALUE
    global ACTUAL_CTR
    global BACKGROUND
    global Efficiency_VALUE
    global previousState
    global currentState
    Eff_Value = 0

    if(ACTUAL_CTR != TARGET_VALUE):
            TimeNow = datetime.datetime.now() 

            currentState = GPIO.input(40)

            if (currentState == 1 and previousState == 0):
                    ACTUAL_CTR=ACTUAL_CTR+1
                    ACTUAL_CTR = int(ACTUAL_CTR)
                    ACTUAL_VALUE.set(ACTUAL_CTR)

            previousState = currentState
            Minutes= getNumberOfMinutes(StartTime,TimeNow)
            if(Minutes < 1):
                    Minutes=1
            if(ACTUAL_CTR >0):
                            Eff_Value = ((ACTUAL_CTR/Minutes)/(TARGET_VALUE/48000))
                            if(Eff_Value>135):
                                    Eff_Value=135
                            Eff_Value = int(Eff_Value)
            Efficiency_VALUE.set(Eff_Value)
            if(Eff_Value >= 80):
                    BACKGROUND = 'green'
            elif(Eff_Value >= 70):
                    BACKGROUND = 'yellow'
            elif(Eff_Value < 60):
                    BACKGROUND = 'red'

            win.configure(background=BACKGROUND)
            win.after(500,task)
    else:
            print "finished"


Label(win,text="%",font=myFont,bg=BACKGROUND).grid(row=2,column=30)
Label(win,text="PCS",font=myFont,bg=BACKGROUND).grid(row=2,column=80)
Label(win,text="",font=myFont,bg=BACKGROUND).grid(row=3,sticky=W)

Label(win,text="Target:",font=myFont,bg=BACKGROUND).grid(row=4)
Label(win,text="75",font=myFont,bg=BACKGROUND).grid(row=4,column=30)
Label(win,text=TARGET_DISPLAY,font=myFont,bg=BACKGROUND).grid(row=4,column=80)
Label(win,text="",font=myFont,bg=BACKGROUND).grid(row=5,sticky=W)

Label(win,text="Actual:",font=myFont,bg=BACKGROUND).grid(row=6)
Label(win,textvariable=ACTUAL_VALUE,font=myFont,bg=BACKGROUND).grid(row=6,column=80)
Label(win,textvariable=Efficiency_VALUE,font=myFont,bg=BACKGROUND).grid(row=6,column=30)

win.after(500,task)
mainloop()

1 个答案:

答案 0 :(得分:0)

您需要做的是为中心栏设置权重。权重用于告诉列/行扩展特定的速率。

将此行添加到win = Tk()下:

win.columnconfigure(1, weight=1)

然后将标签中的列更改为以下内容。

Label(win,text="%").grid(row=2,column=1)
Label(win,text="PCS").grid(row=2,column=2)
Label(win,text="").grid(row=3,sticky=W)

Label(win,text="Target:").grid(row=4)
Label(win,text="75").grid(row=4,column=1)
Label(win,text=TARGET_DISPLAY).grid(row=4,column=2)
Label(win,text="").grid(row=5,sticky=W)

Label(win,text="Actual:").grid(row=6)
Label(win,textvariable=ACTUAL_VALUE).grid(row=6,column=2)
Label(win,textvariable=Efficiency_VALUE).grid(row=6,column=1)

这应将您应该在此处的所有标签移到最右边,然后将中间标签移到屏幕的百分之一。