Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
combobox
#1
I have a problem with a Combobox.
In my program, I want to draw different things depending on what it is selected in the Combobox.

from tkinter import *
from tkinter import ttk
import turtle
from math import cos,sin,tan,pi
from threading import Event

root = Tk()
root.title("Mi app")
stop = Event()

def clear():
  pen.clear()

def graph ():

  chosen = box.get()
  # reset stop event, if it was already set
  stop.clear()
  pen.penup()
  angle = 0
  theta = 0.01
  steps = int ((100*pi/theta))
  a = 1
  b = 1
  for t in range(0,steps):
    if stop.is_set():
      break
    if chosen is "small"():
      b = 2
    if chosen is "big"():
      b = 3
    angle+=theta
    x=(cos(a*angle))*200
    y=(sin(b*angle))*200
 
    pen.goto(x,y)
    pen.pendown()

canvas=turtle.Canvas(master=root, width=650, height=650)
canvas.grid(row=0, column=0,columnspan=1, rowspan=20,padx=5,pady=5)

pen=turtle.RawTurtle(canvas)
pen.width(3)

choose_label=Label(root, text="Choose what you want to draw:")
choose_label.grid(column=1, row=1, columnspan=3, padx=5)
box = ttk.Combobox (root, values = ["small",
                                    "big",])
box.grid(column = 1, columnspan=3, row = 2)
box.current()

graph_button=Button(root, text="Graph", command=graph)
graph_button.grid(row=3,column=1,padx=5,pady=5)

stop_button=Button(root, text="Stop", command=stop.set)
stop_button.grid(row=3,column=2,padx=5,pady=5)

clear_button=Button(root, text="Clear", command=clear)
clear_button.grid(row=3,column=3,padx=5,pady=5)

root.mainloop()
In the first place, is this the way to do a Combobox?
Is there a better way?
box = ttk.Combobox (root, values = ["small",
                                    "big",])
box.grid(column = 1, columnspan=3, row = 2)
box.current()
On the other hand, I know the problem is here.
chosen = box.get()
if chosen is "small"():
   b = 2
if chosen is "big"():
   b = 3
Reply
#2
chosen = box.get()
if chosen is "small"():
   b = 2
if chosen is "big"():
   b = 3
small and big should not have () after them, this is trying to call the string.
check for equality using == not is
Reply
#3
Thanks, that was useful.
Now I have another problem.
I want to create a Label that displays whatever I selected.
I try this:

if box.get() == "small":
  chosen_label = Label(root, text="You choose 'small'")
  chosen_labes.grid(column = 1, row = 4)
I try to put this after all the others buttons but it didn't work.
Is there a way to add a widget depending on what you choose on the combobox?
Reply
#4
Spelling error:
if box.get() == "small":
  chosen_label = Label(root, text="You choose 'small'")
  chosen_labes.grid(column = 1, row = 4)  # chosen_labes?
Didn't python complain? I saw this output in the terminal.
Output:
Traceback (most recent call last): File "C:\Users\djhys\Documents\Python\Musings\junk.py", line 42, in <module> chosen_labes.grid(column = 1, row = 3) NameError: name 'chosen_labes' is not defined
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] How can I sync Combobox index to other combobox index? nickzsche 2 2,329 Jan-03-2022, 12:29 PM
Last Post: Axel_Erfurt

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020