Attribute error in simpledialog module in Python 2 - python-2.7

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 *

Related

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.

python importing Tkinter on PC in command line

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()

AttributeError: 'module' object has no attribute 'Unpickler'

I am running a program in the SciTE text editor and it gets stuck at the import scipy as sp statement.
import pylab as pl
import numpy as np
import scipy as sp
import scipy.integrate as spi
import scipy.optimize as spo
import numpy.random as nr
import matplotlib.pyplot as plt
print "check"
#parameters to be used
rs=0.14
ra=0.0027
js=-0.033
ja=0.81
Mz=91.0
Tz=2.5
root_s=sp.linspace(20.,140.,200) #plotting sigma_A and sigma_S functions against centre of mass energies from 20GeV to 140 GeV
s=(root_s)**2
.....
.....
and I get this error message:
>pythonw -u "collisions.py"
3.14159265359
1.0
Traceback (most recent call last):
File "collisions.py", line 4, in <module>
import scipy as sp
File "C:\Python27\lib\site-packages\scipy\__init__.py", line 85, in <module>
from numpy import oldnumeric
File "C:\Python27\lib\site-packages\numpy\oldnumeric\__init__.py", line 14, in <module>
from compat import *
File "C:\Python27\lib\site-packages\numpy\oldnumeric\compat.py", line 106, in <module>
class Unpickler(pickle.Unpickler):
AttributeError: 'module' object has no attribute 'Unpickler'
>Exit code: 1
The odd thing is that my code works perfectly fine in the interactive qtconsole. I am new to programming, and I have not encountered such a problem elsewhere in the internet. And why is it that the values pi and 1.0 are returned even though there is no where in my code that is supposed to do that?