title: tkinter: When a certain dropdown value is selected, how to make a Button do something else?
body: <p>I'm trying to make a dropdown menu that makes it so when you press the button it will either convert the text entered into binary or the binary entered into text depending on what you have selected in the dropdown menu. I made a simple dropdown menu and have the first value in the list as the default value for the dropdown menu as shown below:</p>
  <pre><code>dropDownItems = [
      'Convert Text to Binary',
      'Convert Binary to Text'
  ]
  
  clicked = tk.StringVar()
  clicked.set(dropDownItems[0])
  
  dropDown = tk.OptionMenu(root, clicked, *dropDownItems)
  dropDown.pack()
  </code></pre>
  <p>Near the end of the program, I made it so that when <code>clicked.set(dropDownItems[0])</code>, it will convert text into binary. This is the default value so in my mind I'm thinking that this should run when the program is ran.</p>
  <pre><code>if clicked.set(dropDownItems[0]):
      # myClick on the left is basically myClick(), but tkinter doesn't want the
      # two parentheses. Change the text to a variable checking what &quot;mode&quot; the
      # thing is in.
      myButton = tk.Button(root, text='Convert Text to Binary', command=myClick)
      myButton.pack(side=BOTTOM, pady=25)
  </code></pre>
  <p>I'm confused because <code>dropDownItems[0]</code> is the default value since I did <code>clicked.set(dropDownItems[0])</code>, so the code above should be running when I run the program (I expected the button to be created when ran, but it isn't created). Sorry if this isn't clear, here is the entire program:</p>
  <pre><code>import tkinter as tk
  import json
  from tkinter import *
  
  root = tk.Tk()
  root.title('BitCoiner') # BinaryTextConverter &gt; B.T.C &gt; BitCoiner (bit as in data
                          # bit)(coiner as in... coiner)
  root.iconbitmap('c:/Users/Duttas/Desktop/Coding/BinaryConverter/test.ico')
  root.geometry('300x300')
  #root.resizable(False, False) # Makes resizing the window impossible.
  
  background = tk.PhotoImage(file='background.gif')
  background_label = tk.Label(root, image=background)
  background_label.place(x=0, y=0, relwidth=1, relheight=1)
  
  root.update() # Ensures that when we call winfo_width that we won't get the
                # wrong width if the root (window) hasn't updated.
  Banner = tk.Label(root, bg='grey' ,text='BitCoiner', width=root.winfo_width(), height=3)
  Banner.pack()
  #new below
  dropDownItems = [
      'Convert Text to Binary',
      'Convert Binary to Text'
  ]
  
  clicked = tk.StringVar()
  clicked.set(dropDownItems[0])
  
  dropDown = tk.OptionMenu(root, clicked, *dropDownItems)
  dropDown.pack()
  #new^
  
  userInput = tk.Entry(root)
  userInput.pack(expand = True, fill='both', padx=10, pady=10)
  
  def binaryEncoder(s=''):
      return [bin(ord(c))[2:].zfill(8) for c in s] # c is character in this case.
                                                   # Ord is ASCII/Bin is Binary.
                                                   # zfill makes a wall of 8x 0s
                                                   # for it to be written over or
                                                   # as someone explained it --
                                                   # &quot;padding&quot; (binary is 8 digits/bit)
  
  def myClick():
      s = userInput.get()
      myLabel = tk.Label(root, text=(binaryEncoder(s))) # Change 'text=' so it
                                                        # creates file with the
                                                        # binary/text data and when
                                                        # you click on it it copys
                                                        # to clipboard.
      root.withdraw()
      root.clipboard_clear()
      root.clipboard_append(binaryEncoder(s))
      root.update
      #with open('convertedBinary.json', 'w') as f:
          #json.dump(binaryEncoder(s), f)
      myLabel.pack()
  
  if clicked.set(dropDownItems[0]):
      # myClick on the left is basically myClick(), but tkinter doesn't want the
      # two parentheses. Change the text to a variable checking what &quot;mode&quot; the
      # thing is in.
      myButton = tk.Button(root, text='Convert Text to Binary', command=myClick)
      myButton.pack(side=BOTTOM, pady=25)
  
  root.mainloop()
  </code></pre>