Member Avatar for sravan953

Hey guys,

I use Python 2.6.2, and have made a program; here's the code:

import subprocess
import time

def open_program():
    time.sleep(s)
    print 'Opening the program...'
    subprocess.call(c)
        
h=input("Time: ")
c=raw_input("Enter the full location of the program you want to open after the desired time: ")
s=(h*60*60)
open_program()

I also have some wxPython code I got from using FarPY GUI:

import sys
import wx
import wx.calendar

class MyFrame(wx.Frame):	
	def __init__(self, parent, title):
		wx.Frame.__init__(self, parent, -1, 'tLapse', wx.DefaultPosition, (560, 472), style=wx.CLOSE_BOX | wx.SYSTEM_MENU | wx.CAPTION | wx.SIMPLE_BORDER | 0 | 0 | wx.MAXIMIZE_BOX | wx.MINIMIZE_BOX)
		self.panel = wx.Panel(self, -1)

		self.textbox1 = wx.TextCtrl(self.panel, -1, 'textbox', (312,64), size=(100, 20))
		self.textbox1.SetBackgroundColour(wx.Colour(255, 255, 255))
		self.textbox1.SetFont(wx.Font(8.25, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, 'Microsoft Sans Serif'))
		self.textbox1.SetCursor(wx.StockCursor(wx.CURSOR_DEFAULT))

		self.label2 = wx.StaticText(self.panel, -1, 'Please enter the desired time you want to lapse(in hours):', (8,64), (300, 20))
		self.label2.SetFont(wx.Font(8.25, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, 'Microsoft Sans Serif'))
		self.label2.SetCursor(wx.StockCursor(wx.CURSOR_DEFAULT))

		self.label4 = wx.StaticText(self.panel, -1, 'Welcome to tLapse. Please select a program you want to open.', (8,16), (325, 17))
		self.label4.SetFont(wx.Font(8.25, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, 'Microsoft Sans Serif'))
		self.label4.SetCursor(wx.StockCursor(wx.CURSOR_DEFAULT))

		self.dialogopen1 = wx.FileDialog(self.panel, message='', defaultDir='', defaultFile='', wildcard='', style=wx.OPEN | wx.CHANGE_DIR)
		self.button2 = wx.Button(self.panel, -1, 'Quit', (448,408), (75, 23))
		self.button2.Bind(wx.EVT_BUTTON,quit)
		self.button2.SetFont(wx.Font(8.25, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, 'Microsoft Sans Serif'))
		self.button2.SetCursor(wx.StockCursor(wx.CURSOR_DEFAULT))

class MyApp(wx.App):
	def OnInit(self):
		frame = MyFrame(None, 'App')
		frame.Show(True)
		self.SetTopWindow(frame)
		return True

def quit(event):
    sys.exit(1)

app = MyApp(True)
app.MainLoop()

What I want to know is, how do I integrate them both? I mean like, how do I make Python accept whatever is entered in the text box in the wxPython? Can anyone please help me?

Thanks a million!

With wxPython your input() and raw_input() are done with the wx.TextCtrl() widget and you need to bind the return key or use a button widget. Also, wxPython has its own timer.


Also, do all of us a favor and don't use those horrible "8 space tabs" for indents!

Take a look at the great wxpython tutorial on this site. It helped me to create some graphical programs with python. You should probably just bind some functions to a button wich is not too hard. I'm not sure how to link to the tutorial but you can find it at the top post in this forum

Member Avatar for sravan953

Take a look at the great wxpython tutorial on this site. It helped me to create some graphical programs with python. You should probably just bind some functions to a button wich is not too hard. I'm not sure how to link to the tutorial but you can find it at the top post in this forum

Can you please specify which link?

Member Avatar for sravan953

How do I bind the return key? I only know to bind a button to a function

Here is an example how to bind the Return/Enter key ...

# testing wxPython's
# wx.TextCtrl(parent, id, value, pos, size, style)
# a widget used for text data input or output

import wx

class MyFrame(wx.Frame):
    def __init__(self, parent, mytitle, mysize):
        wx.Frame.__init__(self, parent, wx.ID_ANY, mytitle, size=mysize)
        self.SetBackgroundColour("yellow")

        s = "Enter name below:"
        edit_label = wx.StaticText(self, wx.ID_ANY, s)
        # create a single line edit input area
        self.edit = wx.TextCtrl(self, wx.ID_ANY, value="")
        # put the cursor in edit
        self.edit.SetFocus()
        # respond to enter key when focus is on edit
        self.edit.Bind(wx.EVT_TEXT_ENTER, self.onEnter)

        # create a scrolled multiline text output area
        self.text_out = wx.TextCtrl(self, wx.ID_ANY, value="",
            style=wx.TE_MULTILINE|wx.HSCROLL|wx.TE_READONLY)
        
        # use a box sizer for vertical widget layout
        sizer_v = wx.BoxSizer(wx.VERTICAL)
        sizer_v.Add(edit_label, 0, wx.LEFT|wx.TOP|wx.EXPAND, border=10)
        sizer_v.Add(self.edit, 0, wx.ALL|wx.EXPAND, border=10)
        sizer_v.Add(self.text_out, 2, wx.ALL|wx.EXPAND, border=10)
        self.SetSizer(sizer_v)

    def onEnter(self, event):
        """the Enter key has been pressed, do something"""
        # GetValue() gives a string
        name = self.edit.GetValue()
        s = "You entered the name: " + name
        # text output
        self.text_out.AppendText(s)
        self.text_out.WriteText('\n')
        # clear edit input space
        self.edit.Clear()


app = wx.App(0)
# create a MyFrame instance and show the frame
MyFrame(None, 'testing wx.TextCtrl()', (300, 480)).Show()
app.MainLoop()

Good start is wxPython's web www.wxpython.org
Then Go to their wiki. If you will walk there there is a link to Jan Bodnar's site www.zetcode.com/wxpython and www.learningpython.org Also there is a mouse vs python (google it) Are great sites for tutorial.

Hard coding is better than GUI Builder, for your code will be cleaner and better documentation :)

Member Avatar for sravan953

What is the

self.edit.SetFocus()

for?

Member Avatar for sravan953

I now have this code:

import sys
import wx
import wx.calendar

class MyFrame(wx.Frame):	
	def __init__(self, parent, title):
		wx.Frame.__init__(self, parent, -1, 'tLapse', wx.DefaultPosition, (560, 472), style=wx.CLOSE_BOX | wx.SYSTEM_MENU | wx.CAPTION | wx.RESIZE_BORDER | wx.FRAME_NO_TASKBAR | 0 | wx.MAXIMIZE_BOX | wx.MINIMIZE_BOX)
		self.panel = wx.Panel(self, -1)

		self.button1 = wx.Button(self.panel, -1, 'Quit', (448,400), (75, 20))
		self.button1.Bind(wx.EVT_BUTTON,exit_now)
		self.button1.SetFont(wx.Font(8.25, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, 'Microsoft Sans Serif'))
		self.button1.SetCursor(wx.StockCursor(wx.CURSOR_DEFAULT))

		self.textbox1 = wx.TextCtrl(self.panel, -1, 'Time in hours', (336,64), size=(100, 20))
		self.textbox1.SetFocus()
		self.textbox1.Bind(wx.EVT_TEXT_ENTER,onEnter)
		self.textbox1.SetBackgroundColour(wx.Colour(255, 255, 255))
		self.textbox1.SetFont(wx.Font(8.25, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, 'Microsoft Sans Serif'))
		self.textbox1.SetCursor(wx.StockCursor(wx.CURSOR_DEFAULT))

		self.label2 = wx.StaticText(self.panel, -1, 'Enter time in hours after which the selected program will open:', (8,64), (316, 17))
		self.label2.SetFont(wx.Font(8.25, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, 'Microsoft Sans Serif'))
		self.label2.SetCursor(wx.StockCursor(wx.CURSOR_DEFAULT))

		self.label3 = wx.StaticText(self.panel, -1, 'Welcome to tLapse. Select the program which you want to open:', (8,16), (332, 23))
		self.label3.SetFont(wx.Font(8.25, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, 'Microsoft Sans Serif'))
		self.label3.SetCursor(wx.StockCursor(wx.CURSOR_DEFAULT))

		self.textbox2 = wx.TextCtrl(self.panel, -1, '', (48,112), size=(279, 146), style=wx.TE_MULTILINE)
		self.textbox2.SetBackgroundColour(wx.Colour(255, 255, 255))
		self.textbox2.SetFont(wx.Font(8.25, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, 'Microsoft Sans Serif'))
		self.textbox2.SetCursor(wx.StockCursor(wx.CURSOR_DEFAULT))

def onEnter(event):
                time_user=self.textbox1.GetValue()
                self.textbox2.WriteText(time_user)

def exit_now(event):
        sys.exit(1)
                
class MyApp(wx.App):
	def OnInit(self):
		frame = MyFrame(None, 'App')
		frame.Show(True)
		self.SetTopWindow(frame)
		return True

app = MyApp(True)
app.MainLoop()

But I am not able to get the program to display what I enter in textbox1 in textbox2...what have I done wrong?

import wx
import wx.calendar

class MyFrame(wx.Frame):	
	def __init__(self, parent, title):
		wx.Frame.__init__(self, parent, -1, 'tLapse', wx.DefaultPosition, (560, 472), style=wx.CLOSE_BOX | wx.SYSTEM_MENU | wx.CAPTION | wx.RESIZE_BORDER | wx.FRAME_NO_TASKBAR | 0 | wx.MAXIMIZE_BOX | wx.MINIMIZE_BOX)
		self.panel = wx.Panel(self, -1)

		self.button1 = wx.Button(self.panel, -1, 'Quit', (448,400), (75, 20))
		self.button1.Bind(wx.EVT_BUTTON, self.exit_now)
		self.button1.SetFont(wx.Font(8.25, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, 'Microsoft Sans Serif'))
		self.button1.SetCursor(wx.StockCursor(wx.CURSOR_DEFAULT))

		self.textbox1 = wx.TextCtrl(self.panel, -1, 'Time in hours', (336,64), size=(100, 20), style=wx.TE_PROCESS_ENTER)
		self.textbox1.SetFocus()
		wx.EVT_TEXT_ENTER(self.textbox1, -1, self.OnEnter)
		 
		self.textbox1.SetBackgroundColour(wx.Colour(255, 255, 255))
		self.textbox1.SetFont(wx.Font(8.25, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, 'Microsoft Sans Serif'))
		self.textbox1.SetCursor(wx.StockCursor(wx.CURSOR_DEFAULT))

		self.label2 = wx.StaticText(self.panel, -1, 'Enter time in hours after which the selected program will open:', (8,64), (316, 17))
		self.label2.SetFont(wx.Font(8.25, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, 'Microsoft Sans Serif'))
		self.label2.SetCursor(wx.StockCursor(wx.CURSOR_DEFAULT))

		self.label3 = wx.StaticText(self.panel, -1, 'Welcome to tLapse. Select the program which you want to open:', (8,16), (332, 23))
		self.label3.SetFont(wx.Font(8.25, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, 'Microsoft Sans Serif'))
		self.label3.SetCursor(wx.StockCursor(wx.CURSOR_DEFAULT))

		self.textbox2 = wx.TextCtrl(self.panel, -1, '', (48,112), size=(279, 146), style=wx.TE_MULTILINE)
		self.textbox2.SetBackgroundColour(wx.Colour(255, 255, 255))
		self.textbox2.SetFont(wx.Font(8.25, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, 'Microsoft Sans Serif'))
		self.textbox2.SetCursor(wx.StockCursor(wx.CURSOR_DEFAULT))

	def OnEnter(self, evt):
                time_user=self.textbox1.GetValue()
		self.textbox1.SetValue("")
                self.textbox2.write(time_user)

	def exit_now(self, event):
		self.Close()
                
class MyApp(wx.App):
	def OnInit(self):
		frame = MyFrame(None, 'App')
		frame.Show(True)
		self.SetTopWindow(frame)
		return True

app = MyApp(True)
app.MainLoop()
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.