首先是我的代码:
from Tkinter import *
def hello():
print "Hello"
root = Tk()
root.attributes('-fullscreen', 1)
icons = []
icons.append(PhotoImage(file="Icons\start.gif"))
icons.append(PhotoImage(file="Icons\quit.gif"))
icons.append(PhotoImage(file="Icons\save.gif"))
icons.append(PhotoImage(file="Icons\load.gif"))
icons.append(PhotoImage(file="Icons\Next.gif"))
screensizex = root.winfo_screenwidth()
screensizey = root.winfo_screenheight()
mainframe = Frame(root, height=(screensizey-(screensizey/20)), width=screensizex, bg="#50a9ad")
mainframe.grid(row=0)
menuframe = Frame(root, height=(screensizey/20), width=screensizex)
menuframe.grid(row=1, sticky="w")
startmenu = Menubutton ( menuframe, text="Start", image=icons[0], compound = LEFT, relief=RAISED,
direction="above")
startmenu.grid(row=0, column=0)
startmenu.place(relx=0.03, rely=0.5, anchor=CENTER)
startmenu.menu = Menu(startmenu, tearoff=0)
startmenu["menu"] = startmenu.menu
startmenu.configure(font=("Arial", 8, "bold"))
startmenu.menu.add_command(label="Next Day", image = icons[4], compound = LEFT, command=hello)
startmenu.menu.add_separator()
startmenu.menu.add_command(label="Save", image = icons[2], compound = LEFT, command=hello)
startmenu.menu.add_command(label="Load", image = icons[3], compound = LEFT, command=hello)
startmenu.menu.add_separator()
startmenu.menu.add_command(label="Quit", image = icons[1], compound = LEFT, command=root.quit)
startmenu.menu.configure(font=("Arial", 8))
root.mainloop()我得到的是:图形用户界面
正如您所看到的那样,菜单“浮动”在菜单按钮之上,而不是仅仅在它上面。我不知道是什么原因造成的,但我不知道如何解决。我相信这很简单,但我是Python的初学者.提前谢谢你的帮助。
发布于 2016-08-18 00:39:44
问题似乎在于将菜单按钮放在绝对底部。下面是几乎最小的代码,菜单按钮从底部向上一行,在Win10上使用3.6 (tk 8.6)。
import tkinter as tk
root = tk.Tk()
root.attributes('-fullscreen', 1)
tk.Button(root, text='Close', command=root.destroy).pack()
mb = tk.Menubutton(root, text='Menu', direction='above')
#mb.pack(side='bottom')
tk.Label(root, text='Filler2').pack(side='bottom')
mb.pack(side='bottom')
tk.Label(root, text='Filler1').pack(side='bottom')
menu = tk.Menu(mb, tearoff=0)
menu.add_command(label='Popup', command=lambda:print('hello'))
mb['menu'] = menu弹出窗口位于Filler1的顶部。将底部向下移动到底部(通过注释和取消注释包行),弹出窗口在相同的位置,留下一个空白。我尝试使用ttk代替,并得到了相同的行为。然后我去了官方传统知识文档,发现了“冲水”的方向,它把盒子放在按钮上。对于tk来说,这意味着“在顶部,以便盖住按钮”。对于ttk来说,它的意思是“冲到顶部,让按钮暴露”,这就是你想要的。因此,解决方案,至少在Windows上使用tk 8.6创建按钮
mb = ttk.Menubutton(root, text='Menu', direction='flush')https://stackoverflow.com/questions/39005823
复制相似问题