1

Verán estoy haciendo mi interfaz en python 3.7 con ayuda de tkinter, y quiero hacerlo vertical, pero como se puede ver en la imagen, las opciones me salen hacia abajo y quiero que salgan hacia la derecha. ¿Es eso posible? Además me gustaría poder editar el tamaño de fuente de las opciones. ¿Existe una forma de hacerlo?

introducir la descripción de la imagen aquí

Este es mi código:

import tkinter
from tkinter import Button
from tkinter import PhotoImage


window = tkinter.Tk()
window.title ("Prototipo")

instrumento = tkinter.StringVar(window)
genero = tkinter.StringVar(window)
compas = tkinter.StringVar(window)
duracion = tkinter.StringVar(window)
velnegra = tkinter.StringVar(window)
instrumento.set("Instrumento Musical")
genero.set("Género Musical")
compas.set("Compás")
duracion.set("Duración")
velnegra.set("Velocidad Negra")


btn_elegirIns = tkinter.OptionMenu(window,instrumento,"Guitarra","Piano")
btn_elegirIns.config(font=("Century Gothic",10),height=2, width=20)
btn_elegirIns.place(x=40,y=100)
btn_generoMus = tkinter.OptionMenu(window,genero, "POP","R&B")
btn_generoMus.config(font=("Century Gothic",10),height=2, width=20)
btn_generoMus.place(x=40,y=150)
btn_elegirCompas = tkinter.OptionMenu(window,compas, "4/4","2/4")
btn_elegirCompas.config(font=("Century Gothic",10),height=2, width=20)
btn_elegirCompas.place(x=40,y=200)
btn_elegirDuracion = tkinter.OptionMenu(window,duracion, "30 segundos","60 segundos")
btn_elegirDuracion.config(font=("Century Gothic",10),height=2, width=20)
btn_elegirDuracion.place(x=40,y=250)
btn_elegirVelNe = tkinter.OptionMenu(window,velnegra, "72","100")
btn_elegirVelNe.config(font=("Century Gothic",10),height=2, width=20)
btn_elegirVelNe.place(x=40,y=300)

window.geometry('950x550') #Definir tamaño de la ventana.
window.resizable(0,0)
window.mainloop()

1 respuesta 1

3

La ubicación del submenú de un OptionMenu, no es configurable, (o al menos no he podido encontrar la forma), pero si puedes modificar el tamaño de la letra. Te sugiero, armar una clase propia para crear estos ítems y configurarlos de manera más simple:

import tkinter
from tkinter import Button
from tkinter import PhotoImage
from tkinter import StringVar

class MyOptionMenu(tkinter.OptionMenu):

    def __init__(self, master, status, x, y, options, func=None):
        self.var = StringVar(master)
        self.var.set(status)
        self.command = self.func if func is None else func
        tkinter.OptionMenu.__init__(self, master, self.var, command=self.command, *options)
        self.config(font=('Century Gothic',(10)),bg='white', height=2, width=20)
        self['menu'].config(font=('Century Gothic',(10)),bg='white')
        self.place(x=x,y=y)

    def func(self,value):
        print(value)

Notas:

  • Con self['menu'].config(font=('Century Gothic',(10)),bg='white') configuramos la letra del ítem
  • options para indicar los ítems, pero habrá que hacerlo como lista
  • func para definir una función a ejecutar cuando se hace click en el ítem

Y para crear los menus:

window = tkinter.Tk()
window.title ("Prototipo")

btn_elegirIns = MyOptionMenu(window,"Instrumento Musical", 40, 100, ["Guitarra","Piano"])
btn_generoMus = MyOptionMenu(window,"Género Musical", 40, 150, ["POP","R&B"])
btn_elegirCompas = MyOptionMenu(window, "Compás", 40, 200, ["4/4","2/4"])
btn_elegirDuracion = MyOptionMenu(window,"Duración", 40, 250, ["30 segundos","60 segundos"])
btn_elegirDuracion = MyOptionMenu(window,"Velocidad Negra", 40, 300, ["72","100"])

window.geometry('950x550') #Definir tamaño de la ventana.
window.resizable(0,0)
window.mainloop()

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.