2017-04-04 50 views
0

我使用TKinter来呈现流行的推文(使用tweepy从推特API)。问题是推文的框架位于彼此的底部。我试图改变框架的一面,但没有任何改变。使用TKinter显示彼此相邻的标签

的问题如下所示

The problem

我想微博是这样彼此相邻:

The desired

这里是我的代码:

from Tkinter import * 
import tweepy 
from local import * 
from PIL import Image, ImageTk 
from ttk import Frame, Style 
import Tkinter as tk 
import ttk 

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) 
auth.set_access_token(OAUTH_TOKEN, OAUTH_TOKEN_SECRET) 
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) 
auth.set_access_token(OAUTH_TOKEN, OAUTH_TOKEN_SECRET) 
api = tweepy.API(auth) 
sa_ID = 23424938 
large_text_size = 12 
text_size = 20 

#create obj in the root page (main windows) 
root= Tk() 
#make bg black 
root.configure(background='black') 
screenWidth = root.winfo_screenwidth() 
screenHeight = root.winfo_screenheight() 

root.overrideredirect(1) 
root.geometry('%dx%d+0+0' % (screenWidth, screenHeight)) 
root.configure(background='#000000') # black 

canvas = Canvas(
    background='#000000', # black 
    borderwidth=-5, 
    height=500, 
    relief='flat', 
    width=500) 

canvas.pack(expand=1, fill=BOTH) 
#create invisible container 
topframe= tk.Frame(root,background='black') 
topframe.pack(side=TOP, fill=BOTH, expand = YES) 
label24 = Label(topframe, text="Popular Tweets", bg='black', fg='white',font=('minionpro', text_size)).pack(anchor=CENTER) 

bottomframe= Frame(root) 
bottomframe.pack(side= BOTTOM) 

#, padx=20, pady=20 
# name the window 
root.title("The news Portal") 
trends1 = api.trends_place(id=sa_ID) 
data = trends1[0] 
# grab the trends 
trends = [] 
trends=data['trends'] 
scrollbar = tk.Scrollbar(root) 
scrollbar.pack(side=RIGHT, fill=Y) 
listbox = Listbox(root, yscrollcommand=scrollbar.set, background='black') 
listbox.pack(side=RIGHT) 
scrollbar.config(command=listbox.yview) 


for trend in trends: 
    if trend['name'].startswith('#'): 
     for status in tweepy.Cursor(api.search, q=trend['name'], result_type='popular').items(1): 
      leftframe = tk.Frame(canvas, background='black', borderwidth=2, relief="groove") 
      leftframe.pack() 
      label6 = Label(leftframe, text=('Tweet by: @' + status.user.screen_name, status.text), bg='black',fg='white', 
           font=('minionpro', large_text_size)).pack(side=BOTTOM,anchor=N) 
      #label23 = Label(leftframe, text="\n", bg='black', fg='white').pack(side=BOTTOM) 


      #print status.text 
#windows is continously there untill uder close it (never close) 
root.mainloop() 

事情是我是mi由于这种布局,发布了很多推文。我很感谢你的帮助。

+0

在SO中搜索'[tkinter]网格标签'返回435个结果。我想象一下,前10或20应该有所帮助。 –

+0

我确实搜索过,并且确实应用了多个解决方案,如使用地点,网格,甚至制作for循环。我没有看到你通过写这个评论来添加新的信息:) @TerryJanReedy –

+0

你没有在这个问题中说这个评论的新信息,所以我不知道。很多初学者显然不知道如何搜索,因为简单的搜索会出现重复。无论如何,您所需的输出是一个固定大小的只读文本小部件的网格。可能是因为推特限制。 “标签”或“文字”可能都有效。后者更复杂,但允许不同的字体和大小。 –

回答

0

使用pack()布局时 - 您放弃了决定帧中元素之间间距的能力。 Tk只是使元素在分配的帧内占用最小的空间量。如果您改为使用grid()布局,则可以在框架中更具体地安排事物,并确定您希望它们占用多少框架。有关示例和用法,请参阅网格的effbot描述:http://effbot.org/tkinterbook/grid.htm

+0

_“当使用pack()布局时 - 你放弃了决定帧之间元素间距的能力。”_ - 这不完全正确。您可以使用选项'padx','pady','ipadx'和'ipady'来添加额外的填充。 –

+0

够公平 - 确切地说要在帧之间放置东西的地方变得有点难。 – paddock

+0

@BryanOakley我试图使用网格,但同样的问题发生。请,你能提出一种方法来达到上面所需的图片吗? :( –