I have just started getting into python and so far it's all been very interesting and the help good to, but now i have ran into a problem. I'm sure it's very easy to solve but i just can't seem to figure it out or find anything on the web.

I have made a GUI and i want to make it so that when a button is pressed it will call my client script and run it, But i have no idea how to add this to the button i have. Any help would be fantastic.

Recommended Answers

All 8 Replies

Since there is little information, I am assuming Windows XP, Tkinter GUI, and Python code client.

Let's say your client code is saved as Client1.py to the working folder and might look like this ...

# print out a given year's monthly calendars

import calendar

calendar.prcal(2005)

# optional console wait for keypress
raw_input('Press Enter...')

... then this Tkinter code should run the client from the code ...

from Tkinter import *

def runClient():
    execfile("client1.py")

form1 = Tk()
push1 = Button(form1, text='Click to run client program', command=runClient)
push1.pack()
form1.mainloop()

Thanks for the reply and sorry forgot to add what i was using for this my bad :sad:

I am using XP, python and Boa-constructor. I have got one more question if anyone can help. I would like to be able to show my cleints output in a window on the GUI rather than have it run in the dos window, i have a scrollable window i just need to add the output some how?

One way to do this would be to save your client output as a text file and then on your end load the file into a string and assign it to the multiline edit window text in your GUI program. I don't know anything about your client, so I have to guess.

:D Thank you loads and loads :D I have got my button and my display working fine thanks to you. Until next time be good :D

I've come across antoher problem now one that i completely forgot about :sad: When i press the start button in my GUI it starts the client sending the packets just fine but then it crashes because there is no way to stop the client from sending packets. I have made a button and tried a few things but can't seem to get it to stop the client script from running. Any ideas at all would be very welcome.

Your client needs something like a KeyboardInterrupt which is normally the control-c or delete key.

try:
  # send data
except KeyboardInterrupt:
  # stop sending data

Just a guess!

OMG! i can't belevie i didn't remember about the keyboard interrupt. Thanks for that, is it possible to make a button click represent the interrupt then? I think it would be nicer to be able to use a button than a keystroke. Still thanks anyway :D

Use the Edit Shell in your BOA Constructor and type
help('wxPython.wx.wxKeyEvent.GetKeyCode')
I think that's what you are looking for. You have to bind that method to your button click event.

The key code globals come up when you type
help('wxPython.wx')

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.