python-3.3

How to insert an image into a button?

こ雲淡風輕ζ 提交于 2019-12-11 08:47:01
问题 I'm executing the following code import tkinter import tkinter.messagebox import random from PIL import Image item = tkinter.Button(root, text=color, width=20, height=10, relief='raised', borderwidth=5, bg=color ) original = Image.open('images/img1.gif') ph_im = Image.PhotoImage(original) item.config(image=ph_im) item.pack(side='left') I am using Pillow for Python33. I'm trying to insert an image into a button, but returns this error message: Traceback (most recent call last): File "C:

01 invalid token [duplicate]

房东的猫 提交于 2019-12-11 03:35:33
问题 This question already has answers here : Why does this dictionary definition raise a syntax error? [duplicate] (3 answers) Invalid Token when using Octal numbers (1 answer) Closed 5 years ago . Hey been learning python3 for a while. Came across dictionaries and the dictionary_name.get() method and tried to get a random key value. The problem: data= {} data.get('key',1) it works and returns 1 But instead if I use data.get('key',01) it says invalid token why is that? 回答1: In Python 2.x, integer

Read the picture as a grayscale numpy array, and save it back

五迷三道 提交于 2019-12-11 00:00:31
问题 I tried the following, expecting to see the grayscale version of source image: from PIL import Image import numpy as np img = Image.open("img.png").convert('L') arr = np.array(img.getdata()) field = np.resize(arr, (img.size[1], img.size[0])) out = field img = Image.fromarray(out, mode='L') img.show() But for some reason, the whole image is pretty much a lot of dots with black in between. Why does it happen? 回答1: When you are creating the numpy array using the image data from your Pillow

Why is concatenating strings running faster than joining them? [duplicate]

旧时模样 提交于 2019-12-10 18:20:12
问题 This question already has answers here : How slow is Python's string concatenation vs. str.join? (5 answers) Closed 6 years ago . As I understand it "".join(iterable_of_strings) is the preferred way to concatenate strings because it allows for optimizations that avoid having to rewrite the immutable object to memory more times than necessary. Adding strings inside of an expression is reliably running faster than joining them for moderately large number of operations for me. I get about 2.9-3

Django admin causing AttributeError

喜欢而已 提交于 2019-12-10 17:57:13
问题 I am learning Django using the Django Book. I am running python3.3.3 on my Macbook Pro with Mavericks 10.9 and when I enable the admin site I get "A server error occurred. Please contact the administrator." in the browser an " AttributeError: 'RegexURLResolver' object has no attribute '_urlconf_module' " error from the Django server. I have checked (and posted) my settings.py and urls.py files and don't see any issues there... I found a similar item on GIT here, but I don't believe it applies

Python except for UnicodeError?

谁都会走 提交于 2019-12-10 17:39:21
问题 In my code I keep getting this error... UnicodeEncodeError: 'charmap' codec can't encode character '\u2013' in position 390: character maps to <undefined> I tried to put an except for UnicodeError and UnicodeEncodeError but nothing works, the problem is it's the users input so I can't control what they put so I need all encode errors to display a print that says error instead of crashing the program... try: argslistcheck = argslist[0] if argslistcheck[0:7] != "http://": argslist[0] = "http://

indentation error with python 3.3 when python2.7 works well

泪湿孤枕 提交于 2019-12-10 15:00:02
问题 I wrote this script below which converts number to it's spelling. no = raw_input("Enter a number: ") strcheck = str(no) try: val = int(no) except ValueError: print("sayi degil") raise SystemExit lencheck = str(no) if len(lencheck) > 6: print("Bu sayi cok buyuk !") raise SystemExit n = int(no) print(n) def int2word(n): n3 = [] r1 = "" ns = str(n) for k in range(3, 33, 3): r = ns[-k:] q = len(ns) - k if q < -2: break else: if q >= 0: n3.append(int(r[:3])) elif q >= -1: n3.append(int(r[:2]))

_socket module import error on fresh python 3.3.5 install

耗尽温柔 提交于 2019-12-10 10:36:25
问题 I installed python 3.35 and I'm getting this error: Traceback (most recent call last): File "C:/Users/Augusto/PycharmProjects/Plot/Database.py", line 48, in <module> import socket File "C:\Python33\lib\socket.py", line 47, in <module> import _socket ImportError: DLL load failed: The specified module could not be found I'm running it on Windows 7 64 bits. I tried both Python installations (Windows x86-64 MSI installer and Windows x86 MSI installer) but both present the same error. I removed

Python console and text output from Ping including \n\r [duplicate]

跟風遠走 提交于 2019-12-10 10:06:06
问题 This question already has answers here : Convert bytes to a string (18 answers) Closed 6 years ago . I dont know what is happening, but when I am printing to the console or to a text file, the newline (\n) is not functioning but rather showing in the string. Any idea how to avoid this in both the console and the text file? My code: import subprocess hosts_file = open("hosts.txt","r") lines = hosts_file.readlines() for line in lines: line = line.strip() ping = subprocess.Popen(["ping", "-n",

How do I gracefully include Python 3.3 from None exception syntax in a Python 3.2 program?

半腔热情 提交于 2019-12-10 09:30:14
问题 I'm trying to re-raise an exception to give the user better information about the actual error. Python 3.3 includes PEP 409. It adds the raise NewException from None syntax to suppress the context of the original exception. However, I am targeting Python 3.2. The Python script will parse, but at runtime if it encounters the from None syntax it will produce TypeError: exception causes must derive from BaseException . For example: try: regex_c = re.compile('^{}$'.format(regex)) except re.error