Python Forum
[Tkinter] Choose from dropdown list and then do something?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Choose from dropdown list and then do something?
#1
Hi, there is an error with my if function... It just woun't write to the document when I press the button "Generate".
I only want to write to a document, a word that is based on the dropdown list, when I press the button "Generate"?
How should I code that?


import tkinter as tk
  
from docx import Document
  
HEIGHT = 500
WIDTH = 600
  
root = tk.Tk()
root.title("Generator")
canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH)
canvas.pack()
  
background_image = tk.PhotoImage(file='bg123.png')
background_label = tk.Label(root, image=background_image)
background_label.place(x=-6, y=-6)
  
#dropdown part
# Create a Tkinter variable
tkvar = tk.StringVar(root)
choices = { 'broker','consultant','recruit'}
tkvar.set('broker') # set the default option
popupMenu = tk.OptionMenu(root, tkvar, *choices)
popupMenu.place(x=260,y=95)
  
def helloCallBack():
   
   if tkvar == 'broker':
      document = Document()
      document.add_heading("broker")
      document.save('0034.docx')
  
button = tk.Button(text="Generate", font=40, command=helloCallBack)
button.place(x=230, y=380)
  
root.mainloop()
Kr.
Selfiatus1
Reply
#2
change
...
def helloCallBack():
    
   if tkvar == 'broker':
        ...
to
...
def helloCallBack():
    
   if tkvar.get() == 'broker':
        ...
Reply
#3
(Jun-07-2019, 07:53 PM)Yoriz Wrote: change
...
def helloCallBack():
    
   if tkvar == 'broker':
        ...
to
...
def helloCallBack():
    
   if tkvar.get() == 'broker':
        ...

THANK YOU!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] choose checkbox devilonline 1 1,239 Feb-17-2023, 01:23 PM
Last Post: Axel_Erfurt
  [Tkinter] Dropdown box showing weird entry cybertooth 4 2,179 Aug-16-2021, 03:45 PM
Last Post: deanhystad
  [PyQt] How do I display the DB table I will choose from the QComboBox in QTableWidget JokerSob 2 2,272 Aug-05-2021, 03:00 PM
Last Post: JokerSob
  Which GUI should I choose ? Matprog49 2 2,299 Apr-25-2020, 02:12 PM
Last Post: Matprog49
  [Tkinter] populating dropdown from Method mikisDW 2 3,815 Apr-06-2020, 08:06 PM
Last Post: mikisDW
  Option dropdown with Pyinquerer julio2000 0 1,512 Mar-22-2020, 04:11 PM
Last Post: julio2000
  Dropdown menu- Store variable aking76 1 3,369 Sep-11-2018, 01:30 PM
Last Post: aking76

Forum Jump:

User Panel Messages

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