2 Replies - 423 Views - Last Post: 10 May 2013 - 07:41 PM Rate Topic: -----

#1 pyr0b0y  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 22-February 09

TKinter Button Question

Posted 10 May 2013 - 05:00 PM

I've been working with TKinter to develop a simple GUI. It will have a couple buttons, and when pressed, run a command in a shell (which is python xxxxx.py). I set up all my relationships how i want to, but want to change the dimension of the button. So far, I have used 'width' which will stretch out the button, but height seems to act the same as padx and pady. I also tried to create to lines to see if I could stretch the height of the button, but that did not work either, any ideas on what I'm doing wrong? I was thinking that maybe using a 25px square image might work, but haven't tried that yet.

I would also like to figure out how to arrange the buttons to exact (x,y) locations within the window.

import subprocess
import os

from Tkinter import *

class App:

    def __init__(self, master):

        frame = Frame(master)
        frame.pack()

        #self.customFont = tkFont.Font(family="Helvetica", size=18)

        self.quitbutton = Button(frame, text="QUIT", command=frame.quit)
        self.quitbutton.pack()

        self.hi_there = Button(frame, text="What is the date?", command=self.action)
        self.hi_there.pack()
        

        self.helpdesk = Button(frame, text="Help Desk", command=self.helpdesk_dash)
        self.helpdesk.pack()

        self.itsystems = Button(frame, text="IT Systems Security", command=self.itsystems_dash)
        self.itsystems.pack()

        self.itfs = Button(frame, text="IT-FS Tasks", command=self.itfs_dash)
        self.itfs.pack()

        self.avops = Button(frame, text="AV/OPS", command=self.av_ops_dash)
        self.avops.pack()

        self.collab = Button(frame, text="Collaboration", command=self.collab_dash)
        self.collab.pack()

        self.logistics = Button(frame, text="Logistics", command=self.logistics_dash)
        self.logistics.pack()

        self.corpnet = Button(frame, text="Corpnet", command=self.corpnet_dash)
        self.corpnet.pack()

        self.deefault = Button(frame, text="Default View", command=self.deefault_dash)
        self.deefault.pack()

    def say_hi(self):
        print "hi there"

    def action(self):
      output = subprocess.check_output ("date", shell = True)
      print (output + 'testing')

    def helpdesk_dash(self):
      hd_button = subprocess.check_output ("python helpdesk_group.py", shell = True)
      print (hd_button)

    def itsystems_dash(self):
      itsystems_button = subprocess.check_output ("python ITSystemsSecurity.py", shell = True)
      print (itsystems_button)

    def itfs_dash(self):
      itfs_button = subprocess.check_output ("python ITFS-Tasks.py", shell = True)
      print (itfs_button)

    def av_ops_dash(self):
      avops_button = subprocess.check_output ("python AV_OPS.py", shell = True)
      print (avops_button)

    def collab_dash(self):
      collab_button = subprocess.check_output ("python Collaboration.py", shell = True)
      print (collab_button)

    def logistics_dash(self):
      logistics_button = subprocess.check_output ("python Logistics.py", shell = True)
      print (logistics_button)

    def corpnet_dash(self):
      corpnet_button = subprocess.check_output ("python Corpnet.py", shell = True)
      print (corpnet_button)

    def deefault_dash(self):
      deefault_button = subprocess.check_output ("python Default_View.py", shell = True)
      print (deefault_button)
   
root = Tk()
root.minsize(300,300)
root.geometry("1200x600")

app = App(root)

root.mainloop() 


Is This A Good Question/Topic? 0
  • +

Replies To: TKinter Button Question

#2 woooee  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 109
  • Joined: 21-November 12

Re: TKinter Button Question

Posted 10 May 2013 - 06:54 PM

The units used in Tkinter depend on the font, so the same height and width will give a different button size for different sized fonts. The following height and width works for me (and the length of code is reduced by using lists).
from Tkinter import *
from functools import partial
import subprocess

class App:

    def __init__(self, master):

        frame = Frame(master)
        frame.pack()

        #self.customFont = tkFont.Font(family="Helvetica", size=18)

        btn_width = 15
        btn_height = 5
        for ctr, txt in enumerate(["What is the date?", "Help Desk",
                                   "IT Systems Security", "IT-FS Tasks",
                                   "AV/OPS", "Collaboration",
                                   "Logistics", "Corpnet",
                                   "Default View"]):
            print ctr, txt
            Button(frame, text=txt, command=partial(self.callback, ctr),
                   width=btn_width, height=btn_height).pack()
        
        self.quitbutton = Button(frame, text="QUIT", command=frame.quit, bg="lightblue").pack()

    def callback(self, ctr):
        action = ["date", "python helpdesk_group.py",
                  "python ITSystemsSecurity.py", "python ITFS-Tasks.py",
                  "python helpdesk_group.py", "python Collaboration.py",
                  "python Logistics.py","python Corpnet.py",
                  "python Corpnet.py"]
        output = subprocess.check_output (action[ctr], shell = True)
        print action[ctr], output
        
    def say_hi(self):
        print "hi there"
 
   
root = Tk()
root.minsize(300,300)
root.geometry("600x900")

app = App(root)

root.mainloop() 

This post has been edited by woooee: 10 May 2013 - 06:56 PM

Was This Post Helpful? 0
  • +
  • -

#3 pyr0b0y  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 22-February 09

Re: TKinter Button Question

Posted 10 May 2013 - 07:41 PM

thanks for the help with that. I haven't looked into callback yet (I just started using Tkitner a day ago) so I'll be looking into it. Would you mind explaining:

 Button(frame, text=txt, command=partial(self.callback, ctr),
                   width=btn_width, height=btn_height).pack()


I know Button (frame, text=txt, command=?????

What does partial do? Or could you point me in the right direction.

Also, if I wanted to make the font size bigger, could I add btn_size=20?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1