简体   繁体   中英

Showing labels in frames next to each other with TKinter

I'm using TKinter to render popular tweets(from twitter API using tweepy). The problem is that the frames of the tweets are positioned at the bottom of each other. I've tried to change the side of the frame but nothing has changed.

The problem is shown below

The problem

I want the tweets to be like this next to each other:

The desired

Here is my code:

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()

The thing is that i'm missing out a lot of tweets because of this layout. I would appreciate your help.

When you use the pack() layout - you give up your ability to decide the spacing between elements in frames. Tk just makes the elements take up the minimum amount of space within their assigned frame. If you instead use the grid() layout, you can arrange things more specifically within frames, and decide how much of the frame you want them to take up. For examples and usage, see the effbot description of grid: http://effbot.org/tkinterbook/grid.htm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM