python-3.x 如何使用tkinter更改下拉菜单的位置

dhxwm5r4  于 2022-11-26  发布在  Python
关注(0)|答案(2)|浏览(308)

我试图使一个简单的下拉图形用户界面,但我需要一些帮助,如何定位下拉菜单,完整的代码是:

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()
hpxqektj

hpxqektj1#

您只需要在此代码的末尾添加以下语句canvas1.create_window(250,250, window=w)

from Tkinter import *

master = Tk()

variable = StringVar(master)
variable.set("one") # default value

w = OptionMenu(master, variable, "one", "two", "three")
w.pack()
lp0sw83n

lp0sw83n2#

您可以使用canvas1.create_window将位置更改为任何适合您的位置。在第23行中,将pack()替换为canvas1.create_window
试试这个:

import tkinter as tk

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 = tk.StringVar(root)
variable.set("Facebook")

w = tk.OptionMenu(root, variable, "Facebook","Twitter","Spotify","Swiggy")
canvas1.create_window(150, 250, window=w)

button1= tk.Button(text='Go')
canvas1.create_window(250, 250, window=button1)

root.mainloop()

结果:

相关问题