【问题标题】:Cannot position widgets with pack无法使用包定位小部件
【发布时间】:2015-05-02 15:25:46
【问题描述】:

所以我有了这个运行良好的简洁小程序,除非我尝试使用 fill 选项将其打包到 tkinter 中。如果我将 fill 选项与 X 值一起使用,则会出现错误。

TclError: bad fill style "64": must be none, x, y, or both

据我所知,tkinter 说我在填充选项中除了 nonexyboth 之外还有其他内容...我尝试将 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'&#039;', "'", caption)
        sub_2 = sub(r'&quot;', '"', sub_1)
        sub_3 = sub(r'&rsquo;', "'", sub_2)
        sub_4 = sub(r'&ldquo;', '"', 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 - 您的绝大多数代码完全无关与打包小部件的问题。另外,不要同时使用gridpack;参见例如stackoverflow.com/q/3966367/3001761
  • @jonrsharpe 不要同时使用gridpack?您至少应该在同一个父级下指定..
  • @Rinzler 会更准确,但总的来说,我认为最好从一个或另一个开始新用户
  • 在这个应用程序中使用 pack 和 grid 非常好(如果不是真的首选!)。只要两个小部件具有不同的父级,您就可以安全地为一个小部件使用网格并为另一个使用打包。

标签: python tkinter fill pack


【解决方案1】:

这就是全局导入不好的原因。 Tkinter 和 re 模块都定义了“常量”X。当您在导入 Tkinter 后导入 re 时,X 被设置为 64,因此出现错误。

最简单的解决方法是删除所有全局导入:

import Tkinter as tk
import re
...
state = tk.Tk()
...

另外,我个人建议 不要 使用 tkinter 常量。它们没有提供真正的价值。只需使用文字字符串"x""y""both"

执行这两个中的任何一个(没有全局导入,不要使用这些常量)都可以防止错误,通常被认为是最佳做法。两者都做,并且有两个定义相同变量的模块,是导致你的问题的原因。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-10
    • 2011-04-04
    • 2023-03-03
    • 2011-04-06
    • 1970-01-01
    • 1970-01-01
    • 2020-12-20
    • 2012-10-14
    相关资源
    最近更新 更多