我试图使一个简单的下拉图形用户界面,但我需要一些帮助,如何定位下拉菜单,完整的代码是:
import tkinter as tk
from tkinter import *
root=tk.Tk()
canvas1 = tk.Canvas(root, width = 400, height = 300)
canvas1.pack()
username = tk.Entry(root)
canvas1.create_window(200,140, window=username)
canvas1.create_text(100,140,fill="darkblue",text="username")
password = tk.Entry(root)
canvas1.create_window(200,180,window=password)
canvas1.create_text(100,180,fill="darkblue",text="password")
variable = StringVar(root)
variable.set("Facebook")
w=OptionMenu(root , variable, "Facebook","Twitter","Spotify","Swiggy")
w.pack()
button1= tk.Button(text='Go')
canvas1.create_window(250,250, window=button1)
root.mainloop()
下拉菜单是通过使用OptionMenu获得的,但我无法更改它的位置,我需要有关OptionMenu代码的帮助:
from Tkinter import *
master = Tk()
variable = StringVar(master)
variable.set("one") # default value
w = OptionMenu(master, variable, "one", "two", "three")
w.pack()
mainloop()
2条答案
按热度按时间hpxqektj1#
您只需要在此代码的末尾添加以下语句
canvas1.create_window(250,250, window=w)
:lp0sw83n2#
您可以使用
canvas1.create_window
将位置更改为任何适合您的位置。在第23行中,将pack()
替换为canvas1.create_window
。试试这个:
结果: