Wednesday, 14 March 2018

Python tkinter advanced gui - Day 19


  • #weight property ,is used so that we can decide which rows and columns should remain less expandable or minimizable when maximizing the window or shrinking it. titles buttons should have less weight where as scroll bar,list should have more weight

  •   
    import tkinter
    import os
    
    mainwindow = tkinter.Tk()
    mainwindow.title("GRID DEMO")
    mainwindow.geometry('600x480-8-200')
    
    label =tkinter.Label(mainwindow,text ='tkinter grid demo label')
    label.grid(row=0,column=0,columnspan = 3)
    
    mainwindow.columnconfigure(0,weight=1)
    mainwindow.columnconfigure(1,weight=1)
    mainwindow.columnconfigure(2,weight=3)
    mainwindow.columnconfigure(3,weight=3)
    mainwindow.columnconfigure(4,weight=3)
    mainwindow.rowconfigure(0,weight=1)
    mainwindow.rowconfigure(1,weight=10)
    mainwindow.rowconfigure(2,weight=1)
    mainwindow.rowconfigure(3,weight=3)
    mainwindow.rowconfigure(4,weight=3)
    
    filelist =tkinter.Listbox(mainwindow)
    filelist.grid(row=1,column=0,sticky = 'nsew',rowspan=2)
    filelist.config(border=2,relief = 'sunken')
    for zone in os.listdir('/Windows/System32/drivers/'):#sample path
        filelist.insert(tkinter.END,zone)
    
    listscroll = tkinter.Scrollbar(mainwindow,orient = tkinter.VERTICAL,command = filelist.yview)
    listscroll.grid(row = 1,column=1,sticky = 'nsw',rowspan=2)
    filelist['yscrollcommand']=listscroll.set
    
    optionframe = tkinter.LabelFrame(mainwindow,text = 'File Details')
    optionframe.grid(row=1,column=2,sticky ='ne')
    
    rbvalue = tkinter.IntVar()
    rbvalue.set(3)#to set the default option for radiobutton
    
    #radio Buttons
    radio1= tkinter.Radiobutton(optionframe,text = 'FileName',value =1,variable =rbvalue)
    radio2= tkinter.Radiobutton(optionframe,text = 'Path',value =2,variable =rbvalue)
    radio3= tkinter.Radiobutton(optionframe,text = 'TimeStamp',value =3,variable =rbvalue)
    radio1.grid(row=0,column=0,sticky ='w')
    radio2.grid(row=1,column=0,sticky ='w')
    radio3.grid(row=2,column=0,sticky ='w')
    
    #widget to display the result
    resultLabel = tkinter.Label(mainwindow,text ="Result")
    resultLabel.grid(row=2,column=2,sticky='nw')
    result = tkinter.Entry(mainwindow)
    result.grid(row=2,column=2,sticky='sw')
    
    #Frame for Time Spinners
    timeFrame = tkinter.LabelFrame(mainwindow,text = 'Time')
    timeFrame.grid(row=3,column=0,sticky = 'new')
    #Time Spinners
    hourSpinner = tkinter.Spinbox(timeFrame,width=2,values = tuple(range(0,24)))
    minuteSpinner = tkinter.Spinbox(timeFrame,width=2,from_=0,to=59)
    secondSpinner = tkinter.Spinbox(timeFrame,width=2,values = tuple(range(0,60)))
    hourSpinner.grid(row=0,column=0)
    tkinter.Label(timeFrame,text=':').grid(row=0,column=1)
    minuteSpinner.grid(row=0,column=2)
    tkinter.Label(timeFrame,text=':').grid(row=0,column=3)
    secondSpinner.grid(row=0,column=4)
    timeFrame['padx']=36
    
    #Frame for Date Spinners
    dateFrame = tkinter.Frame(mainwindow)
    dateFrame.grid(row=4,column=0,sticky = 'new')
    #Date Labels
    dayLabel = tkinter.Label(dateFrame,text='Day')
    monthLabel = tkinter.Label(dateFrame,text='Month')
    yearLabel = tkinter.Label(dateFrame,text='Year')
    dayLabel.grid(row=0,column=0,sticky ='w')
    monthLabel.grid(row=0,column=1,sticky ='w')
    yearLabel.grid(row=0,column=2,sticky ='w')
    #Date Spinners
    daySpin = tkinter.Spinbox(dateFrame,width=5,from_=1,to=31)
    monthSpin = tkinter.Spinbox(dateFrame,width=5,values=("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"))
    yearSpin = tkinter.Spinbox(dateFrame,width=5,from_=2000,to=2099)
    daySpin.grid(row=1,column=0)
    monthSpin.grid(row=1,column=1)
    yearSpin.grid(row=1,column=2)
    
    #buttons
    okButton = tkinter.Button(mainwindow,text = 'OK')
    cancelButton = tkinter.Button(mainwindow,text = 'Cancel',command = mainwindow.destroy)
    okButton.grid(row=4,column=3,sticky='e')
    cancelButton.grid(row=4,column=4,sticky='w')
    
    mainwindow.mainloop()
    print (rbvalue.get())#to confirm the selection of correct value in radio button
     
    

    No comments:

    Post a Comment