【发布时间】:2015-05-02 15:25:46
【问题描述】:
所以我有了这个运行良好的简洁小程序,除非我尝试使用 fill 选项将其打包到 tkinter 中。如果我将 fill 选项与 X 值一起使用,则会出现错误。
TclError: bad fill style "64": must be none, x, y, or both
据我所知,tkinter 说我在填充选项中除了 none、x、y 或 both 之外还有其他内容...我尝试将 x 用小写字母输入,但是那只是将x 称为变量。这只发生在我的代码中,没有其他地方发生,因为我在新文档上进行了实验。
from Tkinter import *
import tkMessageBox
from urllib import *
from webbrowser import *
from re import *
stage = Tk()
button_frame = Frame(stage)
url='http://9gag.com/gif'
caption_var=StringVar()
window_var=StringVar()
def core_fn():
global open_url, read_url, search
global title_head, gif_caption
global counter, caption_count, caption_var
global message_alert, gif_list
counter = 0
#Scrape webpage for gif url
open_url = urlopen(url)
read_url = open_url.read()
search = findall(r'http.*\.gif', read_url)
#Encode all gifs found and append to list
gif_list=[]
for gif in search:
encode_gif = urlopen(gif).read().encode('base64', 'strict')
gif_list.append(encode_gif)
print 'Encode Success'
#Scrape webpage for title and remove unwanted characters
title_head = str(findall(r'<title>.*</title>', read_url))[9:-10]
#Scrape webpage for caption
caption_search = findall(r'alt=".*"', read_url)
caption_search.remove(caption_search[0])
gif_caption=[]
#Remove unwated characters from caption
for caption in caption_search:
sub_1 = sub(r''', "'", caption)
sub_2 = sub(r'"', '"', sub_1)
sub_3 = sub(r'’', "'", sub_2)
sub_4 = sub(r'“', '"', sub_3)
caption = sub_4[5:-1]
gif_caption.append(caption)
#Display retrieved data from webpage
message_alert = 'NOTE: Application is still in beta\n' + str(len(search)) + ' Gifs Found'
caption_var.set(gif_caption[counter])
core_fn()
stage.title(title_head)
def update_gif():
image = PhotoImage(data = gif_list[counter])
label.configure(image=image)
label.image=image
#Previous Function
def btn_previous_fn():
global counter
global search
#Makes sure that the counter is not less than 0
counter -=1
if counter < 0:
#If the counter is less than 0, go to the last gif in the list
counter = (len(search)-1)
#Change and update gif
update_gif()
caption_var.set(gif_caption[counter])
#Keep track of the gif
print 'Gif', str(counter+1), 'of', len(search)
#Next Function
def btn_next_fn():
global counter
global search
#Makes sure that the counter is not greater than the ammount of gifs found
counter +=1
if counter > (len(search)-1):
#If the counter is greater than the ammount of gifs found, go to the first gif in the list
counter = 0
#Change and update gif
update_gif()
caption_var.set(gif_caption[counter])
#Keep track of the gif
print 'Gif', str(counter+1), 'of', len(search)
#Menus
def check():
tkMessageBox.showinfo(title='Check', message=message_alert)
def latest():
global url
url='http://9gag.com/gif/fresh'
core_fn()
update_gif()
stage.title(title_head+' - Latest')
print 'Gif', str(counter+1), 'of', len(search)
tkMessageBox.showinfo(title='Check', message=message_alert)
def popular():
global url
url='http://9gag.com/gif'
core_fn()
update_gif()
stage.title(title_head+' - Popular')
print 'Gif', str(counter+1), 'of', len(search)
tkMessageBox.showinfo(title='Check', message=message_alert)
def save():
url_image = search[counter]
image = PhotoImage(data = gif_list[counter])
gif_name = str(gif_caption[counter]) + '.gif'
image.write(gif_name, format='gif')
tkMessageBox.showinfo(title='Check', message='Save Complete')
#View gif in browser
def web():
open(search[counter])
menu_bar = Menu(stage)
file_menu = Menu(menu_bar)
page_menu = Menu(menu_bar)
about_menu = Menu(menu_bar)
file_menu.add_command(label='Check Page', command=check)
file_menu.add_command(label='Save Image', command=save)
menu_bar.add_cascade(label='File',menu=file_menu)
page_menu.add_command(label='Latest Gifs', command=latest)
page_menu.add_command(label='Popular Gifs', command=popular)
menu_bar.add_cascade(label='Page',menu=page_menu)
about_menu.add_command(label='About')
menu_bar.add_cascade(label='Help',menu=about_menu)
stage.config(menu=menu_bar)
#Initial gif
image = PhotoImage(data = gif_list[counter])
tkMessageBox.showinfo(title='Check', message=message_alert)
print 'Gif', str(counter+1), 'of', len(search)
#Labels
app_title = Label(stage, text=title_head, font=("Impact", 30))
label = Label(stage, image=image)
caption = Label(stage, textvariable=caption_var, wraplength=300, font=("Arial", 11, "italic"))
status_bar = Label(stage, text='Ready', borderwidth=1, relief=SUNKEN, anchor=W)
#Buttons
btn_view = Button(button_frame, command=web, text='View', width=5)
btn_next = Button(button_frame, text='>', width=2, command=btn_next_fn)
btn_previous = Button(button_frame, text='<', width=2, command=btn_previous_fn)
#Place to stage
app_title.pack(side=TOP)
label.pack(side=TOP)
caption.pack(side=TOP)
button_frame.pack(side=TOP)
btn_previous.grid(row=1, column=1)
btn_view.grid(row=1, column=2)
btn_next.grid(row=1, column=3)
#the problem
status_bar.pack(side=BOTTOM, fill=X)
stage.mainloop()
【问题讨论】:
-
请阅读stackoverflow.com/help/mcve - 您的绝大多数代码完全无关与打包小部件的问题。另外,不要同时使用
grid和pack;参见例如stackoverflow.com/q/3966367/3001761 -
@jonrsharpe 不要同时使用
grid和pack?您至少应该在同一个父级下指定.. -
@Rinzler 会更准确,但总的来说,我认为最好从一个或另一个开始新用户
-
在这个应用程序中使用 pack 和 grid 非常好(如果不是真的首选!)。只要两个小部件具有不同的父级,您就可以安全地为一个小部件使用网格并为另一个使用打包。