Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] tkinter, dropdowns with changing options
#1
Helloooooo,

Very new to python. Finding it quite difficult actually.
Learned the fundamentals well enough, but theres never really much instruction on how to fit it all together into something meaningful. Any advice on learning material there owuld be good, but not my problem.

I am currently trying to code a small self-contained application as a project.

the purpose of the application is to help me later and newbies to write puppet manifest files.

So I will have a dropdown box for the type of resource (package, file) and then more drop down boxes to include resource specific attributes.

Would anyone know how to change the options of one dropdown menu, based on the option selected in another or parent one?

Much appreciated.
Quote
#2
show code
Quote
#3
from Tkinter import *
import Tkinter as ttk
from ttk import *
 
root = Tk()
root.title("puppet app")
 
# Add a grid
mainframe = Frame(root)
mainframe.grid(column=0,row=0, sticky=(N,W,E,S) )
mainframe.columnconfigure(0, weight = 1)
mainframe.rowconfigure(0, weight = 1)
mainframe.pack(pady = 100, padx = 100)
 
tkvar = StringVar(root)
 
choices = { 'Package','file'}
tkvar.set('Package') # set the default option
 
popupMenu = OptionMenu(mainframe, tkvar, *choices)
Label(mainframe, text="Choose an option").grid(row = 1, column = 1)
popupMenu.grid(row = 2, column =1)
 

def change_dropdown(*args):
    print( tkvar.get() )
 

tkvar.trace('w', change_dropdown)
 
root.mainloop()
So, when someone selects "package", i need another drop down box to appear or another box to have its options change to the package specific attributes.

This is a puppet manifest in its normal form:
package { 'apache2':
  require => Exec['apt-update'],        # require 'apt-update' before installing
  ensure => installed,
}
So the "require" attribute for package, isnt necessary for the 'file' option.

So at the minute, its almost nothing. just a small GUI box, with one drop down.
I'm thinking the options may change using an IF statement? but i'm not sure how to do it.

Any help appreciated.
Larz60+ wrote Oct-15-2018, 11:17 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

I added for you this time. Please use on all future posts.
Quote
#4
This is a guess as I haven't used tkinter in a while, try changing line 29 to:
tkvar.trace('w', lambda: change_dropdown)
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Error while changing button relief from raised to sunken on left click mgtheboss 1 727 Jan-09-2018, 06:05 PM
Last Post: Gribouillis
  [Tkinter] Trying to display a changing variable AnonymousNobody 9 1,414 Aug-10-2017, 03:52 AM
Last Post: AnonymousNobody
  [Tkinter] Problem with changing label text on button press xk2006x 1 1,798 Jun-02-2017, 06:00 PM
Last Post: nilamo

Forum Jump:


Users browsing this thread: 1 Guest(s)