0

im trying to make a simple dropdown gui,but i need some help on how to position the dropdown menu , the full code is :

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

the dropdown menu was obtained by using the OptionMenu but im unable to change its position, i need help with that code for just the OptionMenu:

from Tkinter import *

master = Tk()

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

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

mainloop()
acw1668
  • 40,144
  • 5
  • 22
  • 34
  • 2
    Why are you unable to change it's position? Where do you want it? It looks like you're succesfully calling `pack`, have you tried the various options to `pack`, or explored using `grid`? – Bryan Oakley Nov 20 '19 at 16:49
  • Read [gui layout using frames and grid](https://stackoverflow.com/questions/34276663/tkinter-gui-layout-using-frames-and-grid/34277295#34277295) – stovfl Nov 20 '19 at 16:58

2 Answers2

0

You just need to add this statement canvas1.create_window(250,250, window=w) at the end of this code:

from Tkinter import *

master = Tk()

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

w = OptionMenu(master, variable, "one", "two", "three")
w.pack()
Tonechas
  • 13,398
  • 16
  • 46
  • 80
  • This doesn't quite solve the problem. Why are you trying to add the `OptionMenu` to a `Canvas` after it has already been added to a `Tk` window? This doesn't explain _how_ to change the `OptionMenu`'s position. See [answer] for more information on writing good answers. – Sylvester Kruin Mar 30 '22 at 21:56
0

You can change position to anywhere to suit you by using canvas1.create_window. In line 23, replace pack() with canvas1.create_window.

Try this:

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

Result:

enter image description here

toyota Supra
  • 3,181
  • 4
  • 15
  • 19