How to design a python gui program [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I currently have a working python code in command line. How can I convert this into a GUI program. I know how to design a GUI(make buttons,callback function, create text field, label widget...). My question is how should be the GUI connected to the existing program. should I make a python file called gui.py and import this in the main program.. ..or should it be in the other way...
eg:
n = int(raw_input('enter an integer: '))
def fx(n):
result = ''
for i in xrange(1,11):
result += "{} x {} = {}\n".format(i,n,i*n)
return result
print fx(n)
the above program will print the multiplication table of an integer. How should be the gui program(with a entry box, button widget, text widget were o/p will be printed). should this program call the GUI code or should I include this code (fx() function) in the GUI class.

As the GUI is the user front-end, and because your function already exists, the easiest is to make GUI class to import the function. On event, the GUI would call the function and handle the display to the user.
In fact, it's exactly what you have done with a Command-Line Interface (CLI) in your example code :)

I would say the answer strongly depends on your choice of GUI-framework to use. For a small piece of code like the one you posted you probably may want to rely on "batteries included" tkinter. In this case I agree to the comment of shaktimaan to simply include the tkinter commands in your existing code. But you have many choices like PyQT, PySide, kivy... All these frameworks have possiblities to seperate programlogic from GUI-view-code, but have different ways to achieve this.
So read about these frameworks if you're not satisfied with tkinter and make a choice, then you can ask again how to do this seperation if you're not sure.

Related

Can I create a simple gui directly on top of my game code? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 months ago.
Improve this question
I am relatively new to coding and I'm currently doing my a level computer science coursework. I'm doing a text based RPG game in python 3. The problem I've ran into is I've coded the entire game with the outputs into the console of VS code, but I need to create a gui for the game. Just a simple window with one textbox that outputs everything the console outputs currently and a textbox that allows for any user input via typing. And maybe a backdrop for the gui. I've scoured the internet for solutions but I cannot find one. The gui don't need to be very fancy. Does anyone have the knowledge of any modules of ways I can easily slap a gui ontop of my code? Thank you in advanced.
For a basic GUI you can use TKinter: Documentation
It's very easy to use and there are a lot example on the web that you can reference
I believe you can use the tkinter (“Tk interface”) package for this. It's a simple and straightforward standard interface in Python to the Tcl/Tk GUI toolkit, and the only GUI framework that comes built into Python itself. Considering your mentioned requirements for the project, you should easily be able to create what you want — an entry widget, for instance (example).

Chess in Python without any game library? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I'm already starting with my project in Python, but before I start, I would like to ask - am I able to create a Chess Game in Python, without the need of downloading any game library (like Pygame)? I should create a game using tkinter.
The interface is actually not related to the chess game at all -
So, you could make a game to play chess with print statements on the terminal, and just use capital and small letters to represent the pieces, like "rkbqkbkr" - and the gameplay part would be independent.
And yes, tkinter is a fully capable windowing toolkit - it only lacks image and text rotation calls - but you can use tkinter to play chess, and won't even need PIL, which is also an external library needed by tkinter to load images, because the chess pieces thenselves are available as unicode characters
♔♕♖♗♘♙♚♛♜♝♞♟ (these also would work for the terminal version)
Moreover, actual serious chess software don't even have to implement an interface at all - there are text-communication protocols that are used by a number of programs which can work in symbiosis - one program, like xchess or gnome-chess working displaying only the graphic interface, and the actual game engine working in text only.
Game libraries do not grow on trees. Someone had to create it first which means theoretically you can write all the code too. But people are using exiting solutions to focus on their creation and not on reinventing the wheels like all the base features each game needs as it takes both skills and time -> it simply "costs". So yes, you can go fully own path. Should you? It's up to you.

How to replace while loop with Tkinter loop [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I was wandering if anyone was able to help me properly add Tkinker into my existing script?. The Tkinker code works fine outside of the current script as does the script without the Tkinter code, but I am having trouble merging it together. been doing alot of searching and i cant seem to fine the answer i am looking for. Here is the link to my script:
https://github.com/Octane70/Code/blob/master/Garden/Garden_v0.3.py
Line 50 #GUI_window is the code i am trying to add.
Thanks
You need to call root.mainloop() exactly once. You need to convert your while True loop into a function, and at the end of the function you need to call itself again with after. This function should also update the GUI, though you could put that in a separate function if you wish.
You do not want to call your gui_display function more than once. As it stands now, you are creating six new widgets every second. Instead, you want to create them once and then update them every second.

Phonebook python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Hi i am new to python an di recently created a phonebook using the dictionary function and then changed into exe using py2exe. I am facing a problem now i enter names in to the phone book and then when i exit the program and return back all the numbers are gone. SO is there any way to save the names and numbers entered into the program? Please give me the code as i am doing this for my class and they would be mad if the numbers disappeared everytime they exited the phonebook! PLEASE HELP!
If you do not yet want to learn relational databases, NoSQL or cloud solutions, you can start by using shelve module.
Well, the main problem is that you do store new values in your database, which is in this case represented with dict, but your don't save it's condition between scripts executions. The time of existing of an object in your script - while the the script is running by interpreter and an object has some links to it. When your restart your program - you're starting to run your script all over again, and it stores in the dict only the elements, which were specified during the script.
The most simple solution, in my opinion, is to use python pickle module. You're gonna save that dict in a file and then load it in the begging of your scrip and save it at the end.
you need to update script code with something like this:
default = {'Sarah': 7736815441,
'John': 7736815442}
def start():
#some code here, before you're trying to access phone numbers in your dict
try:
phonebook = pickle.load(open("data.pb", "r"))
except IOError:
phonebook = default
#your script here
def exit():
#some code here, last chance to modify your dict,
#so changes will appear in next program executions
pickle.dump(phonebook, open("data.pb", "w"))
hope you're familiar with python functions, if no - you can read about then here

How to write GUI in Python? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I would like to try to write a GUI application in Python. I found out that there are a lot of ways to do it (different toolkits). And, in this context, I have several basic (and I think simple) question?
Is it, in general, a good idea to write a GUI application in Python?
What is the standard (easiest and most stable) way to create a GUI applications in Python?
Does anybody can give me a link to a simple Hello World GUI application written in Python?
Depends on what application you are writing. I would use Python for a simple GUI, yes.
Use a proper toolkit (such as PyQt - Python bindings for the popular Qt)
Sure
Hello world in PyQt:
import qt,sys
a = qt.QApplication(sys.argv)
w = qt.QPushButton("Hello World",None)
a.setMainWidget(w)
w.show()
a.exec_loop()
I am really fond of pygtk and glade. Pygtk is a python binding for gtk, the gui toolkit used in gnome. Glade is a user interface designer which stores a gui as xml, which can be loaded in pygtk.
http://www.gtk.org/
http://www.pygtk.org/
http://glade.gnome.org/
If you want to see some example code, you can take a look at my project https://launchpad.net/pumped. Just download the source.
If you're looking to make a fairly simple GUI, then PyGTK is extremely easy to use:
http://www.pygtk.org/
A tutorial (with downloadable sample code) can be found here, and another one on the Wiki.
As an answer for #1: Yes. It is quite good for this; scripting languages with GUI toolkits are often a good way to put a GUI on an application. They can also be used to wrap applications written in low level languages such as C or C++. Python offers good integration to quite a few toolkits. The posting linked above gives a pretty good cross section of the options with code samples.
For #2: TkInter comes with the standard distribution. It is easy to use but not as sophisticated as (say) QT or WxWidgets.
python is ok for gui better than perl and ruby heres some tk
from tkinter import *
from tkinter import ttk
root = Tk()
ttk.Button(root, text="Hello World").grid()
root.mainloop()
If you want to learn some GUI programming in Python using Tkinter, you can see a step-by-step process of building a simple MineSweep clone in twelve progressions here: MineSweep for Python 3.x

Categories

Resources