【问题标题】:Python 2.7.8 Tkinter Gui with image, button and text带有图像、按钮和文本的 Python 2.7.8 Tkinter Gui
【发布时间】:2014-12-07 04:15:23
【问题描述】:

我正在创建一个启动画面,作为我的程序的介绍。我已经设法在屏幕上放置了一个图像,但是一旦我添加了一个按钮,什么都没有出现,甚至没有 tkinter 窗口。

我想要做的是在窗口顶部有一个图像,在它下面有一个我的名字“Bob Johns”的文本框,然后是另一个按钮,上面写着"Enter",它将把用户带到另一个部分程序(即启动应用程序)。所有这些都与中心对齐。

这是我目前所拥有的:

from Tkinter import *
from PIL import ImageTk, Image
import os

#create the window
root = Tk()

#modify root window
root.title("Labeler")
root.geometry("500x500")#Width x Height

img = ImageTk.PhotoImage(Image.open("test.gif"))
panel = Label(root, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")

#If i add this section the program goes awol--------------
app = Frame(root)
app.grid()
button1 = Button(app, text = "This is a button")
button1.grid()
#---------------------------------------------------------


#Start the event loop
root.mainloop()

【问题讨论】:

标签: python image python-2.7 button tkinter


【解决方案1】:

代码混合了packgrid

一次只使用一个布局管理器(至少对于共享相同父级的小部件)

...
img = ImageTk.PhotoImage(Image.open("test.gif"))
panel = Label(root, image = img)
panel.pack(side="top", fill = "both", expand = "yes")

app = Frame(root)
app.pack(side='bottom')
button1 = Button(app, text = "This is a button")
button1.pack()
...

【讨论】:

  • 谢谢!像魅力一样工作。刚刚尝试使用 - master = Tk() w = Label(master, text="Hello, world!") w.pack() 添加文本,但它创建了一个新的 tkinter 窗口,我如何将它放在同一个窗口中? (附言不能让代码工作)
  • @user3423572,请将其作为单独的问题发布。
  • 这是我最初的问题的一部分,但没关系
  • @user3423572,简而言之,重用旧的Tk 对象,而不是创建另一个Tk 对象。 (请参阅this answer 了解如何删除小部件)
  • @user3423572,搜索tkinter splash会给你提示。
猜你喜欢
  • 1970-01-01
  • 2018-08-28
  • 1970-01-01
  • 2015-07-08
  • 2012-09-03
  • 2011-10-11
  • 2013-07-10
  • 1970-01-01
相关资源
最近更新 更多