Python prog to score field archery

Post here if you need help with creating a Graphical User Interface in Python.

Python prog to score field archery

Postby bobnodge » Thu Sep 19, 2013 7:44 pm

Hi I have recently started to learn python programming language & have an idea to build a simple scoring app to record a field archery round I have been picking through several books to try & find out how to go about this using tkinter in python3.3 I have written a few progs to build the basic gui to print the values of the buttons to a console for the first second & third arrows Ie 1-24, 1-20, 1-16, 2-14, 2-10, 3-8, 3-4 by pressing buttons in the master frame however i am having trouble assigning the coresponding values to the buttons to add the various score values together. I have written a basic psudocode txt file that describes what the app shoud do
but have very little idea how to impliment it can anyone help please.
Last edited by Yoriz on Thu Sep 19, 2013 7:53 pm, edited 1 time in total.
Reason: First post lock
bobnodge
 
Posts: 10
Joined: Thu Sep 19, 2013 7:23 pm
Location: Forest of dean glous uk

Re: Python prog to score field archery

Postby Yoriz » Thu Sep 19, 2013 7:54 pm

Hi, Welcome to the forum.
Please ensure you have read the 'new users read this' link in my signature.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 833
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Python prog to score field archery

Postby bobnodge » Thu Sep 19, 2013 8:04 pm

Hi hopefully I can post the psudocode for my question
Code: Select all
from Tkinter import*
import math
class ScoresApp:
   def first_arrow(self,Button):
      Pk=24 [Button, Press_bttn to Add 24 to File]
                      [Add value to Last Entry, Store Total to File]
                      [If no last entry, Store 24 to File]

      K=20  [Button, Press_bttn to Add 20 to File]
           [Add value to Last Entry, Store Total to File]
           [If no last entry, Store 20 to File]

      W=16  [Button, Press_bttn to Add 16 to File]
           [Add value to Last Entry, Store Total to File]
           [If no last entry, Store 16 to File]


   def second_arrow(self,Button):
      K=14  [Button, Press_bttn to Add 14 to File]
           [Add value to Last Entry, Store Total to File]
           [If no last entry, Store 14 to File]

      W=10  [Button, Press_bttn to Add 10 to File]
           [Add value to Last Entry, Store Total to File]
           [If no last entry, Store 10 to File]


   def third_arrow(self,Button):
      K=8   [Button, Press_bttn to Add 8 to File]
           [Add vaule to Last Entry, Store Total to File]
           [If no last entry, Store 8 to File]

      W=4   [Button, Press_bttn to Add 4 to File]
           [Add value to Last Entry, Store Total to File]
           [If no last entry, Store 4 to File]
   def bttn(self,button):
      #First Arrow
      self.bttn1 = Button('Pk'=24)
      self.bttn2 = Button('K'=20)
      self.bttn3 = Button('W'=16)
      #Second Arrow
      self.bttn4 = Button('K'=14)
      self.bttn5 = Button('W'=10)
      #Third Arrow
      self.bttn6 = Button('K'=8)
      self.bttn7 = Button('W'=4)
root=ScoresApp()# Buttons also required for opening a file saving a file
#& displaying a file
bobnodge
 
Posts: 10
Joined: Thu Sep 19, 2013 7:23 pm
Location: Forest of dean glous uk

Re: Python prog to score field archery

Postby stranac » Fri Sep 20, 2013 7:39 am

There are 3 steps you'll need to take to solve this problem:
  • Learn how to use spacing and punctuation
  • Go through a basic tutorial
  • Come back woth an attempt of writing some code
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1144
Joined: Thu Feb 07, 2013 3:42 pm

Re: Python prog to score field archery

Postby bobnodge » Sat Sep 21, 2013 10:32 pm

Thanks for your reply I have been looking through books to get an idea of what the project requires .However I have not yet found all that is needed I have a main window with most of the buttons needed & a module to add a series of numbers together my main difficulty is I cannot find how to use the buttons to generate the scores.
Here is some code I have done to display the buttons.
Code: Select all
from Tkinter import *
class App:
    def __init__(self, master):
        frame = Frame(master)
        frame.pack()
       
        Label(frame, text='First Arrow').pack()
           
        self.bttn1= Button(frame, text="Pk", command=self.bttn1)
        self.bttn1.pack(side=TOP)
        self.bttn2= Button(text='K', command=self.bttn2)
        self.bttn2.pack(side=TOP)
        self.bttn3=Button(text='W', command=self.bttn3)
        self.bttn3.pack(side=TOP)
        separator = Frame(height=3, bd=1, relief=SUNKEN)
        separator.pack(fill=X, padx=5, pady=5)
       
        Label(text='Second Arrow').pack()
        frame=Frame(master)
        frame.pack()
       
        self.bttn4=Button(text='K', command=self.bttn4)
        self.bttn4.pack(side=TOP)
        self.bttn5=Button(text='W', command=self.bttn5)
        self.bttn5.pack(side=TOP)
        separator = Frame(height=3, bd=1, relief=SUNKEN)
        separator.pack(fill=X, padx=5, pady=5)
       
        Label(text='Third Arrow').pack()
        frame=Frame(master)
        frame.pack()
       
        self.bttn6=Button(text='K', command=self.bttn6)
        self.bttn6.pack(side=TOP)
        self.bttn7=Button(text='W', command=self.bttn7)
        self.bttn7.pack(side=TOP)
       
       

       
       
    def bttn1(self):
        print("1-24")


    def bttn2(self):
        print('1-20')

    def bttn3(self):
        print('1-16')
           
    def bttn4(self):
        print('2-14')
       
       
   
    def bttn5(self):
        print('2-10')
       
       
    def bttn6(self):
        print('3-8')
       
       
    def bttn7(self):
      print('3-4')
       
       
       
       
       
root = Tk()
root.geometry('200x400')
app = App(root)
root.mainloop()
Last edited by bobnodge on Sun Sep 22, 2013 6:52 pm, edited 1 time in total.
bobnodge
 
Posts: 10
Joined: Thu Sep 19, 2013 7:23 pm
Location: Forest of dean glous uk

Re: Python prog to score field archery

Postby Yoriz » Sun Sep 22, 2013 9:36 am

Indentation is inportant in python code.
The way you have posted the code all indention is gone.
Please again could you read the 'new user read this' link in my signature and you wil find out how to post your code
to maintain indentation amoungst over usefull information.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 833
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Python prog to score field archery

Postby bobnodge » Sun Sep 22, 2013 6:54 pm

I Have resubmitted the code in my last message it looks better now ,Thank you
bobnodge
 
Posts: 10
Joined: Thu Sep 19, 2013 7:23 pm
Location: Forest of dean glous uk

Re: Python prog to score field archery

Postby stranac » Sun Sep 22, 2013 9:29 pm

You're overwriting the instance variables self.bttn1, self.bttn2... with methods bttn1(), bttn2()...
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1144
Joined: Thu Feb 07, 2013 3:42 pm

Re: Python prog to score field archery

Postby bobnodge » Wed Sep 25, 2013 7:46 pm

I'm afraid I do not see where i have used method calls bttn1(), bttn2()
bobnodge
 
Posts: 10
Joined: Thu Sep 19, 2013 7:23 pm
Location: Forest of dean glous uk

Re: Python prog to score field archery

Postby hrs » Wed Sep 25, 2013 8:41 pm

Code: Select all
    # here you make the variable bttn1
    self.bttn1= Button(frame, text="Pk", command=self.bttn1)
        self.bttn1.pack(side=TOP)

    # here you overwrite it with method bttn1
    def bttn1(self):
        print("1-24")


Just give the method that command refers to a different name.
Code: Select all
import Tkinter

class App():
    def __init__(self, master):
        frame = Tkinter.Frame(master)
        frame.pack()
       
        self.button1 = Tkinter.Button(frame, text = 'Push me', command = self.cmd)
        self.button1.pack()

    def cmd(self):
        print 'This is the command for button1'

root = Tkinter.Tk()
app = App(root)
root.mainloop()
hrs
 
Posts: 86
Joined: Thu Feb 07, 2013 9:26 pm

Re: Python prog to score field archery

Postby bobnodge » Fri Sep 27, 2013 10:17 pm

Thank you hrs your reply is very helpful, much apprieciated I'll have another go at my code.
bobnodge
 
Posts: 10
Joined: Thu Sep 19, 2013 7:23 pm
Location: Forest of dean glous uk

Re: Python prog to score field archery

Postby bobnodge » Sun Sep 29, 2013 8:54 pm

I have modified the code to display two buttons when I try to run it I only get the last print statement.
However I have used the same self.cmd line as shown in your reply which is probably why.
My question is if I change the lines to self.cmd0 & self.cmd1 surely I am simply overwriting the .cmd method
instead of the .bttn method? So how can I make the two buttons display thier coresponding print statements.
bobnodge
 
Posts: 10
Joined: Thu Sep 19, 2013 7:23 pm
Location: Forest of dean glous uk

Re: Python prog to score field archery

Postby hrs » Sun Sep 29, 2013 10:29 pm

Without you showing any code it's impossible for anyone to help you.
Generally it's prefered that you do not provide all of your code, unless your code is short, but instead a dummy version of your program of approximately 10 lines that reproduces the problem. The expected and actual output are important as well.
hrs
 
Posts: 86
Joined: Thu Feb 07, 2013 9:26 pm

Re: Python prog to score field archery

Postby bobnodge » Mon Sep 30, 2013 8:22 pm

I will attempt to post my code again, the prob as stated is that if I run this code it only displays the last print staement
Code: Select all
from Tkinter import *

class App1:
    def __init__(self, master):
        frame = Frame(master)
        frame.pack()
        bttn = Button(frame, text='Pk', command=self.cmd)
        bttn.pack()
        bttn1 = Button(frame, text='k1',command=self.cmd)
        bttn1.pack()
       
    def cmd(self):
      print('1-24', (24))
      
    def cmd(self):
      print('1-20', (20))


root = Tk()
root.wm_title('Kill Button')
root.geometry('200x400')
app = App1(root)
root.mainloop()
bobnodge
 
Posts: 10
Joined: Thu Sep 19, 2013 7:23 pm
Location: Forest of dean glous uk

Re: Python prog to score field archery

Postby hrs » Mon Sep 30, 2013 9:02 pm

Please use code tags next time.

You are now defining cmd twice. The first definition is short lived because you overwrite it immediately with another definition. Different variables and methods should have different and unique names.
Something like this
Code: Select all
from tkinter import *

class App1:
    def __init__(self, master):
        frame = Frame(master)
        frame.pack()
        bttn = Button(frame, text='Pk', command=self.showScore24)
        bttn.pack()
        bttn1 = Button(frame, text='k1',command=self.showScore20)
        bttn1.pack()

    def showScore24(self):
        print('1-24', (24))

    def showScore20(self):
        print('1-20', (20))


root = Tk()
root.wm_title('Kill Button')
root.geometry('200x400')
app = App1(root)
root.mainloop()


By the way, what python version are you using? Your print() usage suggests python 3. But the import Tkinter suggests python 2. At least on my system I need to import tkinter with a lower case when using python 3.
hrs
 
Posts: 86
Joined: Thu Feb 07, 2013 9:26 pm

Re: Python prog to score field archery

Postby bobnodge » Tue Oct 01, 2013 3:44 pm

Thanks hrs I am using python 3.3 with Geany if I use IDLE I also have to use tkinter in lower case, I think the default python for Geany 1.23.1 is V2.7 although I am not very conversant with the different versions of the various IDEs on linux systems. I find that Geany is better for me because it gives you a visual reference to the line numbers of progs when trying to figure out why your code does not do what you expect.
In reference to your code I thought it might be something like that .
bobnodge
 
Posts: 10
Joined: Thu Sep 19, 2013 7:23 pm
Location: Forest of dean glous uk

Re: Python prog to score field archery

Postby bobnodge » Thu Oct 03, 2013 9:59 pm

Hi I have found & modified another prog to add numbers entered from the keyboard. My problem now is how to use this with the buttons in my other code, how to add input from the bttns instead of the keyboard
and associate the corresponding values (ie: 24 for the pk bttn & 20 for the k1 bttn)
I will try again to upload the code. I have trouble trying to use the code tag it simply adds the code to the message as txt

Code: Select all
#addsum.py

total=0
print('Enter a number or ("q")')
s=input()
#Enter a number
while s != ("q"):
      
 num =int(s)
 total = total + num
      
 s =input()
print('The sum is ' + str(total))
bobnodge
 
Posts: 10
Joined: Thu Sep 19, 2013 7:23 pm
Location: Forest of dean glous uk


Return to GUI

Who is online

Users browsing this forum: No registered users and 1 guest

cron