我正在使用TKinter来呈现流行的推文(来自Twitter API使用tweepy)。问题是推文的框架位于彼此的底部。我试图改变框架的一面但没有任何改变。
问题如下所示
我希望推文彼此相邻:
这是我的代码:
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()
事情是,由于这种布局,我错过了很多推文。我很感激你的帮助。
答案 0 :(得分:0)
当你使用pack()布局时 - 你放弃了决定帧中元素之间间距的能力。 Tk只是使元素占用其指定帧内的最小空间量。如果您改为使用grid()布局,则可以在帧中更具体地排列内容,并确定您希望它们占用多少帧。有关示例和用法,请参阅grid:http://effbot.org/tkinterbook/grid.htm
的effbot描述