python importing Tkinter on PC in command line - python-2.7

This is my python program:
from Tkinter import *
root = Tk()
When I do this, it gives me this error message:
Traceback (most recent call last):
File "", line 1, in
File "/usr/lib64/python2.6/lib-tk/Tkinter.py", line 1643, in init
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable
What do i do?

It's more likely to be like this:
from Tkinter import *
root = Tk()
If you want it in online it should be like this:
from Tkinter import * ;root = Tk()

Related

Attribute error in simpledialog module in Python 2

I have to create popup dialog box which contains text field using tkinter module in Python 2(not Python 3). My other program has many Python2 modules(I have written whole code in Python 2) and so I can't go for Python 3. Here is my code which works fine in Python 3 but not in Python 2.
from tkinter import simpledialog
from tkinter import *
def s():
print(simpledialog.askstring("hai","inp"))
root = Tk()
b = Button(root, text="popup",command=s)
b.pack()
root.geometry("400x400")
root.mainloop()
This is the error :
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1550, in __call__
return self.func(*args)
File "popup.py", line 4, in s
print(simpledialog.askstring("hai","inp"))
AttributeError: 'module' object has no attribute 'askstring'
Please mention any alternatives to achieve this function in Python 2.
Thank you
Just change
from tkinter import simpledialog
from tkinter import *
to
import tkSimpleDialog as simpledialog
from Tkinter import *

I am trying to play music using tkinter and module mp3 play but it says theres no module named mp3 play(module downloaded)

Although I have downloaded the module mp3play and saved it in python folder.
from Tkinter import *
import mp3play
root = Tk() # create tkinter window
f = mp3play.load('OnOurWay.mp3'); play = lambda: f.play()
button = Button(root, text = 'Play', command = play)
button.pack()
root.mainloop()
I got this error message:
Error:
Traceback (most recent call last):
File "E:/ON F/Tkinter programs/mujic.py", line 2, in <module>
import mp3play
ImportError: No module named mp3play

Cannot import Tkinter font object from module

I've got a feeling this will end in a facepalm, but I just can't puzzle out why this import x from y scenario isn't working for me. As a starting point, take the following module main.py:
import Tkinter as tk
import tkFont
__metaclass__ = type
#Create widgets
root = tk.Tk()
fntPlainText = tkFont.Font(family='Courier New',size=10)
lblDisplay = tk.Label(root, relief=tk.SUNKEN,
font=fntPlainText,
width=30,height=5,
text='This is a test!')
#Do layout
lblDisplay.grid(padx=5,pady=5)
#Begin app
tk.mainloop()
This runs normally and puts up a small label with the expected content:
So now I want to move the definition of this Font object to a separate module, so I can share it among multiple applications. My updated code modules are fonts1.py:
import Tkinter as tk
import tkFont
__metaclass__ = type
fntPlainText = tkFont.Font(family='Courier New',size=10)
...and main1.py:
import Tkinter as tk
from fonts1 import fntPlainText
__metaclass__ = type
#Create widgets
root = tk.Tk()
lblDisplay = tk.Label(root, relief=tk.SUNKEN,
font=fntPlainText,
width=30,height=5,
text='This is a test!')
#Do layout
lblDisplay.grid(padx=5,pady=5)
#Begin app
tk.mainloop()
But when I run main1.py, I get a traceback:
Traceback (most recent call last):
File "G:\Python\experiments\investigations\tk\main1.py", line 2, in <module>
from fonts1 import fntPlainText
File "G:\Python\experiments\investigations\tk\fonts1.py", line 5, in <module>
fntPlainText = tkFont.Font(family='Courier New',size=10)
File "C:\Python27\lib\lib-tk\tkFont.py", line 89, in __init__
tk.call("font", "create", self.name, *font)
AttributeError: 'NoneType' object has no attribute 'call'
From Googling and experimenting, I found enough to know that something's going wrong with the assignment of fntPlainText, but (again) I can't figure out what it is. The biggest puzzle of the whole situation is that when I pare down fonts1.py to simply define fntPlainText as a tuple--
Modified fonts1.py:
fntPlainText = ('Courier New',10)
Running main1.py now completes normally and paints the same window as main.py.
If it matters, the environment is Python 2.7.x running on Windows 7.
You need to create a root window (tk.Tk()) before creating the font. If you move your
from fonts1 import fntPlainText
below the tk.Tk() call it should work as you expect.

how to Install xvfb and run with python and selenium?

I'm trying to use Xvfb to run headless browser. following process which I followed so far
1. Installed xvfb sudo apt-get install xvfb
2. Created virtualenv,
3. Installed xvfbwrapper
4. run following code
import unittest
from selenium import webdriver
from xvfbwrapper import Xvfb
class TestPages(unittest.TestCase):
def setUp(self):
self.xvfb = Xvfb(width=1280, height=720)
self.addCleanup(self.xvfb.stop)
self.xvfb.start()
self.browser = webdriver.Firefox()
self.addCleanup(self.browser.quit)
def testUbuntuHomepage(self):
self.browser.get('http://www.ubuntu.com')
self.assertIn('Ubuntu', self.browser.title)
def testGoogleHomepage(self):
self.browser.get('http://www.google.com')
self.assertIn('Google', self.browser.title)
if __name__ == '__main__':
unittest.main(verbosity=2)
But I'm getting following error, Even I tried installing this with sudo but no effect.
Traceback (most recent call last):
File "xvfbwrapper.py", line 4, in <module>
from xvfbwrapper import Xvfb
File "/home/ubuntu/unclescrooz/src/robinhood/xvfbwrapper.py", line 4, in <module>
from xvfbwrapper import Xvfb
ImportError: cannot import name Xvfb
Same issue with pyvirtualdisplay
Traceback (most recent call last):
File "pyvirtualdisplay.py", line 1, in <module>
from pyvirtualdisplay import Display
File "/m4k/projects/scrapper/stock/robinhood/pyvirtualdisplay.py", line 1, in <module>
from pyvirtualdisplay import Display
ImportError: cannot import name Display
With following code
from pyvirtualdisplay import Display
from selenium import webdriver
display = Display(visible=0, size=(800, 600))
display.start()
browser = webdriver.Firefox()
browser.get('http://www.google.com')
print browser.title
browser.quit()
display.stop()
Rename your files xvfbwrapper.py and pyvirtualdisplay.py to something else than the name of the module you import.

creating an executable of a python application using py2exe

I'm stuck with creating an executable of my python program. My setup for creating an executable is as shown below,
from distutils.core import setup
import py2exe, sys, os
sys.argv.append('py2exe')
project = dict(script="smarttester.py",
dest_base="Smart Tester",
uac_info="requireAdministrator")
console = [project]
setup(
version = "1.0.0",
description = "executable with privileges",
console = console,
options = {'py2exe': {'bundle_files': 1}},
zipfile = None
)
I'm receiving an error showing as,
Traceback (most recent call last):
File "C:\mypath\to\createexe.py", line 14, in <module>
options = {'py2exe': {'bundle_files': 1}},
File "C:\Python27\lib\distutils\core.py", line 162, in setup
raise SystemExit, error
SystemExit: error: MSVCP90.dll: No such file or directory
Looking for a solution.Thanks in advance.