calling a python script on button click using python and tkinter - python-2.7

I have a python script which has the functionality of sending an email to a user. I executed this script and it is working fine. In another python script I have only a button, so when I click on this button I want the other python script which sends a email to be executed.I have written the following code:
#!/usr/bin/python
import sys
import os
import Tkinter
import tkMessageBox
top=Tkinter.Tk()
def helloCallBack():
os.system('SendEmail.py')
B=Tkinter.Button(top,text="hello",command= helloCallBack)
B.pack()
top.mainloop()
I get the following error when I click on the button:
sh: 1:SendEmail.py:not found.
Could you let me know what is the reason for this error and how it can be resolved.Thanks.

I was able to figure out a way to call another python script on button click:
instead of using os.system('SendEmail.py') we need to use os.system('python SendEmail.py')

import sys
import os
from tkinter import *
window=Tk()
window.title("Running Python Script")
window.geometry('550x200')
def run():
os.system('opencv_video.py')
btn = Button(window, text="Click Me", bg="black", fg="white",command=run)
btn.grid(column=0, row=0)
window.mainloop()

If your SendEmail.py is in the same location, use os.system('SendEmail.py'). If it's in a different location, use os.system('python SendEmail.py').

#!/usr/bin/python
import sys
import sys
import os
import Tkinter
import tkMessageBox
top=Tkinter.Tk()
def helloCallBack():
os.system('python SendEmail.py')
B=Tkinter.Button(top,text="hello",command= helloCallBack)
B.pack()
top.mainloop()
use the keyword "python" to run the command

As an amateur, I am not really qualified to give advice. This is how I did it.
I want to do this kind of thing too. I have about 16 little python programs which make html, sets of checkboxes, sets of radiobuttons, text input fields, html tables etc.
In another thread here a comment was quite deprecative of using os.system calls. Not sure why, but I thought I would try another approach.
I've just started learning tkinter, so I am making each of my 'makehtml' functions run in a window.
Now I want a master window with buttons. Click a button and another window opens, say the checkboxes window, or any of the other windows for making html.
I made a module: guiHTML.py All my 'makehtml' functions are in there.
Import guiHTML in the master window.
import os, sys
# to import the files we need the paths
path = '/home/pedro/myPython/myModules/'
# append the paths
sys.path.append(path)
import tkinter as tk
from functools import partial
import guiHTML
Then, in the master window make a function like this for each button:
def openCheckboxes():
#call the checkboxes function defined in the guiHTML module
guiHTML.checkboxes()
Then, in the checkboxes button just put this:
btn3 = tk.Button(frame1, text='insert checkboxes', command=openCheckboxes)
btn3.grid(columnspan=2, column=0, row=2, sticky='w', pady=10)
Click btn3 and the checkboxes window opens.
This works for me, but I don't know if it is a good way to do this. I only began with tkinter a month ago.
If there is a better way to do this, I'd be glad to hear it from you experts!

#!/usr/bin/python
import sys
import os
import tkinter as tk
root = tk.Tk()
def helloCallBack():
os.system('call.py')
#Keep_both_files_in_the_same_Folder
b1=tk.Button(root, text="Calendar",bg="white",command=helloCallBack)
b1.pack()
root.mainloop()

Related

Debugging while developing Django apps

I'm aware of pdb built-in Python library for debugging Python programs and scripts. However, you can't really use it for debugging Django apps (I get errors when I try to use it in views.py). Are there any tools that I can use when Django's traceback isn't helpful ?
EDIT:
from .forms import TestCaseForm, TestCaseSuiteForm
from .models import TestCase, TestSuite
from django.contrib.auth.forms import UserCreationForm
from django.views.generic import FormView, ListView
from django.contrib.auth.models import User
from django.utils.decorators import method_decorator
from django.contrib.auth.decorators import login_required
from django.contrib.auth import logout
import pdb
class ListAllTestSuites(ListView):
template_name = 'list.html'
context_object_name = 'username'
def get_queryset(self):
pdb.set_trace() # <-- setting a trace here to diagnose the code below
queryset = {'test_suites': TestSuite.objects.filter(user=self.request.user),
'username': self.request.user}
return queryset
you forgot the exact error message and full traceback, and, more importantly, you forgot to explain how you executed this code to get this result...
But anyway: from the error message, you're obviously trying to execute your view file as a plain python script (cf the reference to __main__). This cannot work. A view is a module, not a script, and, moreover, any module dependending on Django needs some setup done to be imported (which is why we use the django shell - ./manage.py shell - instead of the regular Python one).
For most case, you can just launch the django shell, import your module and use pdb.runcall() to execute some function / method under the debugger (no need to put a breakpoint then, but that's still possible).
Now views require a HTTPRequest as first argument which make them a bit more cumbersome to debug that way (well, there is django.tests.RequestFactory but still...), so your best bet here is to set your breakpoint, launch your devserver (or restart it - if it didn't already did, as it should), point your browser to the view's url, and then you should see the debugger's prompt in your devserver's terminal.

Open link using Selenium on new page

I am clicking the link "Images" on a new page (after searching 'bugs bunny') on Google. It is not retrieving images of the search, rather it is opening the link 'Images' on the old page.
My Code:
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
browser = webdriver.Firefox()
browser.get('http://www.google.com')
search = browser.find_element_by_name('q')
search.send_keys("bugs bunny")
search.send_keys(Keys.RETURN) # hit return after you enter search text
browser.current_window_handle
print(browser.current_url)
browser.find_element_by_link_text("Images").click()
Your problem is you are using send_keys, which perform the action and don't wait
search.send_keys(Keys.RETURN) # hit return after you enter search text
So after that if you use click it is doing it nearly on the current page even when the results are not loaded. So you need to add some delay for the return key to change the results and once the results are loaded, you can do the click
So what you need is a simple sleep delay

StaleElementReferenceException occurs during scraping infinite scroll with Selenium in Python

I am trying to scroll down an infinite scroll page and get the links of news. The problem is when I scrolled down the page for let say 100 times, and I tried to get the links, Python launched an error that says: "StaleElementReferenceException: Message: stale element reference: element is not attached to the page document". I think its because the page is get updated and scrolled page is not available any more. here is my code for scrolling the page with Selenium Webdriver:
import urllib2
from bs4 import BeautifulSoup
from __future__ import print_function
from selenium import webdriver #open webdriver for specific browser
from selenium.webdriver.common.keys import Keys # for necessary browser action
from selenium.webdriver.common.by import By # For selecting html code
import time
driver = webdriver.Chrome('C:\\Program Files (x86)\\Google\\Chrome\\chromedriver.exe')
driver.get('http://seekingalpha.com/market-news/top-news')
for i in range(0,100):
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(15)
URL = driver.find_elements_by_class_name('market_current_title')
print URL
and the code for getting the URLs
for a in URL:
links = a.get_attribute('href')
print(links)
I am wondering if there is any solution to settle this problem or it is possible to get URLs for this specific page with request library, as I couldn't do that.

Close the message button

import os
import Tkinter
gui = Tkinter.Tk()
gui.geometry("300x200")
messagebutton1 = Tkinter.Button(gui,text='Process Completed')
messagebutton1.place(x=80,y=80)
This is my example.I need to close this "process Completed" message button by clicking.What is the syntax for that.can you please guide me.
Use destroy method to close Tk.
import os
import Tkinter
def close():
gui.destroy()
gui = Tkinter.Tk()
gui.geometry("300x200")
messagebutton1 = Tkinter.Button(gui,text='Process Completed', command=close)
messagebutton1.place(x=80,y=80)
gui.mainloop()

How to transfer value from one python script class to another python script

I need to connect two python script class to each other and transfer values.
It seems that I made some mistakes to initialize the class objects and passing the
values please note the way I followed and somebody please kindly advise me on where I am getting wrong.
what is wrong with this line of code
def TransferTreeVal(objSubS):
objCM=MainScript.clsMain()
print "Transfer value"
Some more detailed code
##MainScript.py
import os
from Tkinter import *
import Tkinter as tk
import ttk
class clsMain():
def __init__ (objCM):
root['width']=500
root['height']=400
root['bg']='brown'
objCM.MethodDisplay()
def MethodDisplay(objCM):
print "display windows"
root=tk.Tk()
objCM = clsMain()
root.mainloop()
##SubScript.py
import os
from Tkinter import *
import Tkinter as tk
import ttk
import MainScript
class clsSubS():
def __init__ (objSubS):
root['width']=500
root['height']=500
root['bg']='brown'
objSubS.DispWin()
def TransferTreeVal(objSubS):
objCM=MainScript.clsMain()
print "Transfer value"
root=tk.Tk()
objSubS = clsSubS()
The main thing you are doing wrong is that you are importing files that have executable code in them. When you import SubScript.py, it is going to execute the code at the bottom which creates an instance of Tk. However, your main script also creates an instance of Tk and you should only ever have a single instance in a running program.
Normally, if you have a class you want to import, but you also want to use it as a standalone script, you "hide" the standalone script code behind a check like this:
if __name__ == "__main__":
root = tk.Tk()
objSubS = clsSubS()
With that, the code will only get executed if you do python SubScript.py. Also, when you import SubScript, that code will not run.