How to add a GUI to my current code?

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

How to add a GUI to my current code?

Postby acetrainerkirby » Wed Apr 16, 2014 11:02 pm

I have enough experience with Python to write this code in the 2.7.5 version though building a GUI that will read the raw_input text and allow for the user to input an answer is beyond me, I am completely clueless. I followed a guide for Tkinter though it only allowed me to make a window appear, no strings or raw_input was read off. This may be simple, I would just like some help, thanks!


Code: Select all
import ctypes
ctypes.windll.kernel32.SetConsoleTitleA("Pokemon Damage Calculator!")

print ("Welcome, here we will calculate battle damage caused by pokemon!")

level = int( raw_input("What is the level of your pokemon?"))

attack = int( raw_input ("What is the attack stat of your pokemon?"))

power = int( raw_input ("What is the power of your pokemon's attack?"))

defense = int( raw_input("What is the defense stat of your opponent's pokemon?"))

stab = float( raw_input("Does STAB apply? If yes make the variable 1.5, if no make the variable 1."))

resist = float( raw_input("Does the opposing pokemon have a weakness or resistance? If no then answer with 1 if yes : Resistance x4 = 0.25, x2 = 0.5 . Weakness x2 = 2, x4 = 4."))
                 
damage = ((((2 * level / 5 + 2) * attack * power / defense) / 50) + 2) * stab * resist * 100 / 100

critical = int( raw_input("Was the attack a critical? If no then answer with 1, if yes than 2."))

total = damage * critical

print "The damage the opponent's pokemon will recieve is %s" % (total)

print str("Thanks for using this program!")

wait = input("PRESS ENTER TO CONTINUE.")
Last edited by stranac on Wed Apr 16, 2014 11:06 pm, edited 1 time in total.
Reason: First post lock.
acetrainerkirby
 
Posts: 4
Joined: Wed Apr 16, 2014 10:55 pm

Re: How to add a GUI to my current code?

Postby metulburr » Wed Apr 16, 2014 11:22 pm

You need to create an Entry inside the window, something like:
Code: Select all
try:
    import Tkinter as tk
except ImportError:
    import tkinter as tk

class Application:
    def __init__(self, parent):
        self.myentrybox = tk.Entry(parent)
        self.myentrybox.pack()
        self.myentrybox.insert(0,"default")
        self.myentrybox.bind("<Return>",self.enter)

    def enter(self,event):
        print("You entered >> {}".format(self.myentrybox.get()))

root = tk.Tk()
app = Application(root)
root.mainloop()
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1415
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: How to add a GUI to my current code?

Postby acetrainerkirby » Wed Apr 16, 2014 11:32 pm

Thanks for replying! Where would I go about placing my code so that my raw_input questions are shown and can be answered through the box in the code given above?
acetrainerkirby
 
Posts: 4
Joined: Wed Apr 16, 2014 10:55 pm

Re: How to add a GUI to my current code?

Postby metulburr » Thu Apr 17, 2014 12:23 am

You can use the kinter Label
http://effbot.org/tkinterbook/label.htm

Code: Select all
try:
    import Tkinter as tk
except ImportError:
    import tkinter as tk
   
QUESTIONS = [
    'What is your favorite color?',
    'How old are you?'
]

class Application:
    def __init__(self, parent):
        self.myentrybox = tk.Entry(parent)
        #self.myentrybox.insert(0,"default")
        self.myentrybox.bind("<Return>",self.enter)
       
        self.questions = [
            'What is your favorite color?',
            'How old are you?'
        ]

        self.text = tk.StringVar()
        self.text.set(self.questions[0])
        self.label = tk.Label(parent, textvariable=self.text).pack()
        self.myentrybox.pack()

    def enter(self,event):
        print("You entered >> {}".format(self.myentrybox.get()))
        self.switch_question()
        self.myentrybox.delete(0, tk.END)
       
    def switch_question(self):
        ind = 0
        for q in self.questions:
            if q == self.text.get():
                ind = self.questions.index(q) + 1
        if ind > len(self.questions)-1:
            ind = 0
        self.text.set(self.questions[ind])

root = tk.Tk()
app = Application(root)
root.mainloop()
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1415
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: How to add a GUI to my current code?

Postby acetrainerkirby » Thu Apr 17, 2014 12:33 am

Thanks so much! I will tinker with this and see what I can do, thanks again! :D
acetrainerkirby
 
Posts: 4
Joined: Wed Apr 16, 2014 10:55 pm

Re: How to add a GUI to my current code?

Postby acetrainerkirby » Thu Apr 17, 2014 12:47 am

Okay, so this should be my last question, how would I go about taking the data answered by the user and use it in an equation and have it only display that answer instead of each individual answer given by the user? Thanks so much for your help!
acetrainerkirby
 
Posts: 4
Joined: Wed Apr 16, 2014 10:55 pm


Return to GUI

Who is online

Users browsing this forum: No registered users and 2 guests