tkinter标签离开屏幕。无论如何要发现这个

时间:2017-05-31 16:11:27

标签: python tkinter grid label

我有一些python代码使用循环将tkinter标签放在问题的画布上。我的问题是,如果数组被打印出12个标签,有时它们适合屏幕,有时如果标签包含大文本则不会。

如果屏幕宽度已用尽,是否有一种聪明的方法可以打破此循环

l_row_start_display=6 
for l in G_DATA_VIEW_LABELS_ARRAY: ## list of lists of labels
      l_cnt=0
      for lab in l:
         l_align='W'             
         lab.grid(row=l_row_start_display,
                column=l_cnt,sticky=l_align) 
         l_cnt+=1
      l_row_start_display+=1

或任何其他想法

感谢

2 个答案:

答案 0 :(得分:0)

编辑:

我在使用.grid()放置标签的同时找到了一种方法。我们可以在man窗口中使用框架,然后使用winfo_width()winfo_height()获取框架的大小。使用此信息并在每个标签生成后获取框架的高度和宽度,然后我们可以决定何时结束while语句。放置标签时,这将给我们一个粗略的准确度。唯一的问题是它会测量当前的帧大小而不是小部件的大小,因此您可能需要使用数字来将其移动到您想要的位置。

以下是示例:

from tkinter import *

root = Tk()
root.geometry("500x300")

l = ["SOME TEXT", "SOME TEXT", "SOME TEXT", "SOME TEXT", "SOME TEXT", "SOME TEXT", "SOME TEXT", "SOME TEXT", "SOME TEXT", "SOME TEXT", "SOME TEXT", "SOME TEXT", "SOME TEXT", "SOME TEXT", "SOME TEXT", "SOME TEXT"]
screen = root.winfo_width()
my_frame = Frame(root)
my_frame.pack(expand=True)

last_row = 0
last_column = 0
my_frame_x = my_frame.winfo_width()
my_frame_y = my_frame.winfo_height()
b = True
while b == True:
    for stuff in l:
        if my_frame_x > 450 or my_frame_y > 250:
            print ("Labels cannot be outside of window!")
            b = False
        else:
            lablabel = Label(my_frame) 
            lablabel.config(text = stuff)         
            lablabel.grid(row = last_row, column = last_column)
            last_row += 1
            last_column += 1
            my_frame.update()
            my_frame_x = my_frame.winfo_width()
            my_frame_y = my_frame.winfo_height()

root.mainloop()

您也可以使用.place()布局管理器执行此操作。您需要做的就是定义您希望在其中创建标签的最大像素x和y。

例如,我有一个设置为800x300的窗口,并且我已经将标签生成器配置为在达到像素限制后不再创建标签。这是通过定义标签的像素大小,将该大小添加到下一个位置然后检查以确保新的x和y数字不超过预定义的大小来完成的。

看一下这个例子:

from tkinter import *

root = Tk()
root.geometry("800x300")
root.minsize(800, 300)


l = ["SOME TEXT", "SOME TEXT", "SOME TEXT", "SOME TEXT", "SOME TEXT", "SOME TEXT", "SOME TEXT", "SOME TEXT", "SOME TEXT", "SOME TEXT", "SOME TEXT", "SOME TEXT", "SOME TEXT", "SOME TEXT", "SOME TEXT", "SOME TEXT", "SOME TEXT", "SOME TEXT", "SOME TEXT", "SOME TEXT", "SOME TEXT", "SOME TEXT", "SOME TEXT"]
last_x = 0
last_y = 0
b = True
while b:
    for stuff in l: 
        last_x+=100
        last_y+=30
        if last_x > 700 or last_y > 270:
            b = False
            print ("Labels cannot be outside of window!")

        else:
            lablabel = Label(root) 
            lablabel.config(text = stuff)         
            lablabel.place(x = last_x, y = last_y, width = 100) 

root.mainloop()

答案 1 :(得分:-1)

测量字体是关键。

from __future__ import print_function
from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools

try:
    import argparse
    flags = argparse.ArgumentParser(parents=
[tools.argparser]).parse_args()
except ImportError:
    flags = None

SCOPES = 'https://www.googleapis.com/auth/calendar'
store = file.Storage('storage.json')
creds = store.get()
if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('client_secret.json', 
SCOPES)
    creds = tools.run_flow(flow, store, flags) \
          if flags else tools.run(flow, store)
CAL = build('calendar', 'v3', http=creds.authorize(Http()))

GMT_OFF = '-04:00'          # ET/MST/GMT-4
EVENT = {
    'summary': 'Buy apples',
    'start': {'dateTime': '2017-06-24T13:00:00%s' % GMT_OFF},
    'end': {'dateTime': '2017-06-24T14:00:00%s' % GMT_OFF},
}

e = CAL.events().insert(calendarId='primary',
                        sendNotifications=True, body=EVENT).execute()

print('''*** %r event added:
    Start: %s
    End: %s''' % (e['summary'].encode('utf-8'),
                  e['start']['dateTime'], e['end']['dateTime']))