添加框架小部件时 Tkinter gui 停止运行
Tkinter gui stops functioning when adding frame widget
这是我在项目中使用的一些代码,它非常简单,目前无法正常工作。我在使用框架小部件时遇到问题。现在当我 运行 代码没有任何反应,但是如果我删除关于框架和滚动条的行,并将 listbox/canvas 小部件父级更改为 "root" 而不是 "frame",代码有效。我在网上看了很多例子,但我不明白为什么当我试图把所有东西都放在一个框架而不是根 window 时没有 gui window 出现......?谢谢!
使用 python 2.7.
import pymongo
import glob, os
import Tkinter
from Tkinter import *
from PIL import Image, ImageTk
root = Tkinter.Tk()
root.title("keyBuilder")
frame = Frame(root, width = 630, height = 400)
groupMenu = Listbox(frame)
groupMenu.config(width = 30)
groupMenu.insert(END, "Group 1")
groupMenu.insert(END, "Group 2")
groupMenu.insert(END, "Group 3")
yscrollbar = Scrollbar(frame)
yscrollbar.grid(row=0, column=1, sticky=N+S)
gallery = Canvas(frame, width = 600, height = 400, yscrollcommand = yscrollbar.set)
path = r'/Users/Sam/Desktop/keynoteProject/slideIcons'
COLUMNS = 6
image_count = 0
for infile in glob.glob(os.path.join(path, '*.jpeg')):
image_count += 1
r, c = divmod(image_count-1, COLUMNS)
im = Image.open(infile)
resized = im.resize((100, 100), Image.ANTIALIAS)
tkimage = ImageTk.PhotoImage(resized)
myvar = Label(gallery, image=tkimage)
myvar.image = tkimage
myvar.grid(row=r, column=c)
groupMenu.pack(side = LEFT, fill = Y)
gallery.pack(side = RIGHT)
frame.pack()
root.mainloop()
如果有帮助,此代码有效:
import pymongo
import glob, os
import Tkinter
from Tkinter import *
from PIL import Image, ImageTk
root = Tkinter.Tk()
root.title("keyBuilder")
groupMenu = Listbox(root)
groupMenu.config(width = 30)
groupMenu.insert(END, "Group 1")
groupMenu.insert(END, "Group 2")
groupMenu.insert(END, "Group 3")
gallery = Canvas(root, width = 600, height = 400)
path = r'/Users/Sam/Desktop/keynoteProject/slideIcons'
COLUMNS = 6
image_count = 0
for infile in glob.glob(os.path.join(path, '*.jpeg')):
image_count += 1
r, c = divmod(image_count-1, COLUMNS)
im = Image.open(infile)
resized = im.resize((100, 100), Image.ANTIALIAS)
tkimage = ImageTk.PhotoImage(resized)
myvar = Label(gallery, image=tkimage)
myvar.image = tkimage
myvar.grid(row=r, column=c)
groupMenu.pack(side = LEFT, fill = Y)
gallery.pack(side = RIGHT)
root.mainloop()
问题是您将 pack
和 grid
混合在同一个容器中。对于共享同一父级的所有小部件,您只能使用一个或另一个。 groupMenu
、yscrollbar
和 canvas
都是 frame
的子代。 yscrollbar
使用 grid
但其他人使用 pack
.
这是我在项目中使用的一些代码,它非常简单,目前无法正常工作。我在使用框架小部件时遇到问题。现在当我 运行 代码没有任何反应,但是如果我删除关于框架和滚动条的行,并将 listbox/canvas 小部件父级更改为 "root" 而不是 "frame",代码有效。我在网上看了很多例子,但我不明白为什么当我试图把所有东西都放在一个框架而不是根 window 时没有 gui window 出现......?谢谢!
使用 python 2.7.
import pymongo
import glob, os
import Tkinter
from Tkinter import *
from PIL import Image, ImageTk
root = Tkinter.Tk()
root.title("keyBuilder")
frame = Frame(root, width = 630, height = 400)
groupMenu = Listbox(frame)
groupMenu.config(width = 30)
groupMenu.insert(END, "Group 1")
groupMenu.insert(END, "Group 2")
groupMenu.insert(END, "Group 3")
yscrollbar = Scrollbar(frame)
yscrollbar.grid(row=0, column=1, sticky=N+S)
gallery = Canvas(frame, width = 600, height = 400, yscrollcommand = yscrollbar.set)
path = r'/Users/Sam/Desktop/keynoteProject/slideIcons'
COLUMNS = 6
image_count = 0
for infile in glob.glob(os.path.join(path, '*.jpeg')):
image_count += 1
r, c = divmod(image_count-1, COLUMNS)
im = Image.open(infile)
resized = im.resize((100, 100), Image.ANTIALIAS)
tkimage = ImageTk.PhotoImage(resized)
myvar = Label(gallery, image=tkimage)
myvar.image = tkimage
myvar.grid(row=r, column=c)
groupMenu.pack(side = LEFT, fill = Y)
gallery.pack(side = RIGHT)
frame.pack()
root.mainloop()
如果有帮助,此代码有效:
import pymongo
import glob, os
import Tkinter
from Tkinter import *
from PIL import Image, ImageTk
root = Tkinter.Tk()
root.title("keyBuilder")
groupMenu = Listbox(root)
groupMenu.config(width = 30)
groupMenu.insert(END, "Group 1")
groupMenu.insert(END, "Group 2")
groupMenu.insert(END, "Group 3")
gallery = Canvas(root, width = 600, height = 400)
path = r'/Users/Sam/Desktop/keynoteProject/slideIcons'
COLUMNS = 6
image_count = 0
for infile in glob.glob(os.path.join(path, '*.jpeg')):
image_count += 1
r, c = divmod(image_count-1, COLUMNS)
im = Image.open(infile)
resized = im.resize((100, 100), Image.ANTIALIAS)
tkimage = ImageTk.PhotoImage(resized)
myvar = Label(gallery, image=tkimage)
myvar.image = tkimage
myvar.grid(row=r, column=c)
groupMenu.pack(side = LEFT, fill = Y)
gallery.pack(side = RIGHT)
root.mainloop()
问题是您将 pack
和 grid
混合在同一个容器中。对于共享同一父级的所有小部件,您只能使用一个或另一个。 groupMenu
、yscrollbar
和 canvas
都是 frame
的子代。 yscrollbar
使用 grid
但其他人使用 pack
.