How do I enable multisampling using glfw in Python? - python

I am trying to enable 4x multisampling for OpenGL in Python. But I keep getting an error saying "GFLW_SAMPLES" is not defined. The way I understand it this should be included in glfw.
from OpenGL.GL import *
import glfw
import sys
glfw.window_hint(GLFW_SAMPLES, 4)
Is there another library I have to import for this?

You have 2 possibilities:
import glfw
glfw.window_hint(glfw.SAMPLES, 4)
or
from glfw.GLFW import *
glfwWindowHint(GLFW_SAMPLES, 4)

Related

import syntax in Python

Is there any difference between coding:
import turtle
and coding
from turtle import *
(Using module turtle as an example, I suppose the answer will be true for all modules.)
My understanding was that the * imports all methods in the module, so why use the longer syntax?

Using the wrong module

I'm programming a game with pygame, but when I try to use the python's math module, my program tries to use pygame.math instead. How can I solve this issue?
Example:
import pygame, math
from pygame import *
pygame.init()
a = math.sin(90)
# some code that uses a
Error message:
AttributeError: module 'pygame.math' has no attribute 'sin'
The error might be because of from pygame import *, but my code doesn't work without it.
Solved in the commments:
Use the code as following, as it will prevent errors:
import pygame
import math
pygame.init()
a = math.sin(90)
This way it imports as separate modules. You had it saying:
Import Pygame and Pygame Math
Initiate Pygame
define variable "a"
I use pygame and math in my code but I have them as separate imports:
import pygame
import math
And everything works as expected without error

Import order in Python 3

I'm having a few issues with a number of imports in my program,
In main.py:
from world import *
from dialogue import *
from event import *
In dialogue.py:
from world import *
from event import *
The class Area is defined in world.py, yet when I try to use the Area class from dialogue.py it returns
builtins.NameError: name 'Area' is not defined
If I change the order of the imports in main.py to
from dialogue import *
from world import *
from event import *
When I try to access the dialogue class from world.py, I get this
builtins.NameError: name 'Dialogue' is not defined
I thought the order of imports shouldn't have made a difference? How can I access all of my classes from all of my files?
The class Area is defined in world.py, yet when I try to use the Area class from dialogue.py it returns
The way you are importing your code is wrong. From both modules you are importing with *; this confuses the Python, because both modules have a class called Area.
Instead of using * (wild import) import them as modules
import dialogue
import world
import event
d1 = world.Dialogue()
d2 = dialogue.Dialogue()

getting python executable or running on the web

I currently have a python script that runs on my local machine windows 7. I am using the following
from scipy.optimize import fsolve
import ctypes
import matplotlib.pyplot as plt
import numpy as np
import os, fnmatch
from scipy.signal import argrelextrema
from matplotlib.patches import Rectangle
from reportlab.pdfgen import canvas
from reportlab.lib import colors
from reportlab.lib.units import mm, inch
from reportlab.lib.pagesizes import A3, A4,landscape,letter, inch,portrait
from reportlab.platypus import Image
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, Image, Paragraph
import subprocess
import sys
getting an executable is a challenge. Even If I do there are all these .dll files and dependencies. Now I am thinking how difficult it would be to get my code to run on the web? Or do I abandon python and go for something else like java, C#, Delphi or ? A Gui would be nice to have since currently it is console driven. My program goes as follows. A user enters something then the program will calculate something report back to the user. Then the user will enter another parameter and the program will calculate something else. A table and graph is produced but the graph is not necessary.
The goal is to make it simple for the user to use.
Any help is much appreciated.
See using Python on the web. From your imports it looks complicated enough to warrant doing it on the backend (server side). So I recommend sticking with Python.
I was able to get an executable using cx_Freeze (cx_Freeze-4.3.4-cp27-none-win_amd64). I am using python 2.7.9. I used the following setup.py script
import cx_Freeze
import Tkinter as Tk
import ctypes
import numpy
import matplotlib
import os, fnmatch
import sys
import subprocess
import reportlab
base = None
if sys.platform == 'win32':
base = "Win32GUI"
executables = [
cx_Freeze.Executable("Test.py", base=base),
]
cx_Freeze.setup(
name = "Testv1.0",
options = {"build_exe":{"packages":["Tkinter", "matplotlib"], "includes":["FileDialog"], "excludes":[]}},
version = "0.1",
description = "MyApp",
executables = executables
)
Now what was key was adding "matplotlib" inside "packages":["Tkinter", "matplotlib"] and also having "includes":["FileDialog"], in the options. This resolved any errors I was having while trying to create the .exe.

Import * statement in Python

I thought that
from tkinter import *
imports all names into my current file's namespace so that I can access all of it directly. However, I get an error on instantiating a message box:
messagebox.showinfo("Something")
Once I add
from tkinter import messagebox
all works fine. I don't understand why. Didn't the first import statement already import all names in the tkinter module including messagebox?
Importing a module (tkinter) does not automatically import submodules (tkinter.messagebox) unless the module do it explicitly for you.
messagebox is a submodule of tkinter.
You should import module "messagebox"
(use "import ... as ..." to make it shorter)
import tkinter.messagebox
tkinter.messagebox.showinfo("Something")
Or as you figured out yourself,
from tkinter import messagebox
Since messagebox is a file inside the Tkinter module, you won't be able to access it by just calling Tkinter. To import the submodules you have to call out the specific files like this:
from tkinter import messagebox

Categories

Resources