Register
Email: Password:
Forum » General Programming thread
  • « previous
  • 1
  • 2
  • 3
  • » next
  • General Programming thread

    Anarion 16 years ago
    Ok, so who knows about Python? I have a program here that I'm writing for class, but for some reason the bloody canvas won't work and I can't work out why.

    from Tkinter import *
    from tkColorChooser import *
    import time
    import random
    import sys
    import pickle

    rectStatus =0
    ovalStatus =0
    text1Status =0
    polyStatus =0

    #Control Window#
    class Control(Frame):
    def __init__(self, master=None):
    Frame.__init__(self, master)
    self.grid()
    self.createWidgets()

    def createWidgets(self):
    top=self.winfo_toplevel()
    top.rowconfigure(0, weight=1)
    top.columnconfigure(0, weight=1)
    self.rowconfigure(0, weight=1)
    self.columnconfigure(0, weight=1)

    ##Temperature CB##
    self.cbTemp = Checkbutton(self,
    relief=RAISED,
    selectcolor='Green',
    text="Temperature",
    indicatoron=0,
    anchor=N)
    self.cbTemp.grid(row=1,
    column=1,
    sticky=N+S+E+W,
    padx=2,
    pady=2)

    ##Pressure CB##
    self.cbPressure = Checkbutton(self,
    relief=RAISED,
    selectcolor='Green',
    text="Pressure",
    indicatoron=0,
    anchor=N)
    self.cbPressure.grid(row=2,
    column=1,
    sticky=N+S+E+W,
    padx=2,
    pady=2)

    ##Humidity CB##
    self.cbHumidity = Checkbutton(self,
    relief=RAISED,
    selectcolor='Green',
    text="Humidity",
    indicatoron=0,
    anchor=N)

    self.cbHumidity.grid(row=3,
    column=1,
    sticky=N+S+E+W,
    padx=2,
    pady=2)

    ##Wind Speed CB##
    self.cbSpeed = Checkbutton(self,
    relief=RAISED,
    selectcolor='Green',
    text="Wind Speed",
    indicatoron=0,
    anchor=N)

    self.cbSpeed.grid(row=4,
    column=1,
    sticky=N+S+E+W,
    padx=2,
    pady=2)

    ##Rainfall CB##
    self.cbRainfall = Checkbutton(self,
    relief=RAISED,
    selectcolor='Green',
    text="Rainfall",
    indicatoron=0,
    anchor=N)

    self.cbRainfall.grid(row=5,
    column=1,
    sticky=N+S+E+W,
    padx=2,
    pady=2)

    ##Wind Direction CB##
    self.cbDirection = Checkbutton(self,
    relief=RAISED,
    selectcolor='Green',
    text="Wind Direction",
    indicatoron=0,
    anchor=N)

    self.cbDirection.grid(row=6,
    column=1,
    sticky=N+S+E+W,
    padx=2,
    pady=2)



    def set_partner(self,pf):
    self.pf = pf

    #Display Window#
    class Display(Frame):
    def __init__(self,parent):
    global widget


    Frame.__init__(self,parent,bg='Green')

    parent.wm_geometry(newGeometry="+10+10")
    parent.wm_title("Display Panel")
    parent.config(bg='Grey')

    self.cvs = Canvas(self,
    background="blue",
    width=400,
    height=400,
    borderwidth=15,
    relief='groove')



    ##Pressure Button##
    self.pressurebutton = Button(self.cvs,
    text="Pressure",
    bd=5,
    padx=2,
    pady=2,
    relief=GROOVE)
    self.pressurebutton.pack(anchor=NW)
    self.cvs.create_window(50, 50, window=self.pressurebutton,anchor=NW)

    ##Wind Indicator##
    self.rectID= self.cvs.create_rectangle(
    50,150, #x1,y1,
    200,300, #x2,y2,
    fill="White", #fill color
    outline="Black",
    tags='Rect'); #outline color
    #Pointer N
    self.North= self.cvs.create_polygon(
    125,150,
    140,170,
    130,170,
    130,200,
    120,200,
    120,170,
    110,170,
    fill="Black", #fill color
    outline="Blue", #outline color
    tags="North");
    #Pointer S
    self.South= self.cvs.create_polygon(
    125,300,
    140,280,
    130,280,
    130,250,
    120,250,
    120,280,
    110,280,
    fill="Black", #fill color
    outline="Blue", #outline color
    tags="South");
    #Pointer E
    self.East= self.cvs.create_polygon(
    200,225,
    180,240,
    180,230,
    150,230,
    150,220,
    180,220,
    180,210,
    fill="Black", #fill color
    outline="Blue", #outline color
    tags="East");

    #Pointer W
    self.West= self.cvs.create_polygon(
    50,225,
    70,240,
    70,230,
    100,230,
    100,220,
    70,220,
    70,210,
    fill="Black", #fill color
    outline="Blue", #outline color
    tags="West");

    #Pointer SW
    self.SouthWest= self.cvs.create_polygon(
    60,290,
    60,260,
    70,270,
    100,240,
    110,250,
    80,280,
    90,290,
    fill="Black", #fill color
    outline="Blue", #outline color
    tags="SW");
    #Pointer SE
    self.SouthEast= self.cvs.create_polygon(
    190,290,
    190,260,
    180,270,
    150,240,
    140,250,
    170,280,
    160,290,
    fill="Black", #fill color
    outline="Blue", #outline color
    tags="SE");

    #Pointer NE
    self.NorthEast= self.cvs.create_polygon(
    190,160,
    190,190,
    180,180,
    150,210,
    140,200,
    170,170,
    160,160,
    fill="Black", #fill color
    outline="Blue", #outline color
    tags="NE");
    #Pointer NW
    self.NorthWest= self.cvs.create_polygon(
    60,160,
    60,190,
    70,180,
    100,210,
    110,200,
    80,170,
    90,160,
    fill="Black", #fill color
    outline="Blue", #outline color
    tags="NW");

    #Text N
    self.N = self.cvs.create_text(120,150,
    text="N" ,
    fill="Blue",
    anchor=SW,
    font=("Comic Sans Ms",10),
    tags="N");
    #Text S
    self.S = self.cvs.create_text(120,320,
    text="S" ,
    fill="Blue",
    anchor=SW,
    font=("Comic Sans Ms",10),
    tags="S");
    #Text W
    self.W = self.cvs.create_text(30,235,
    text="W" ,
    fill="Blue",
    anchor=SW,
    font=("Comic Sans Ms",10),
    tags="W");
    #Text E
    self.E = self.cvs.create_text(210,235,
    text="E" ,
    fill="Blue",
    anchor=SW,
    font=("Comic Sans Ms",10),
    tags="E");
    #Text Wind Direction
    self.text3 = self.cvs.create_text(60,350,
    text="Wind Direction" ,
    fill="Blue",
    anchor=SW,
    font=("Comic Sans Ms",12,"italic"),
    tags="lable2");




    pw = Toplevel()
    pf = Display(pw)
    cw = Control()
    #cw.set_partner(pf)
    pw.mainloop()
    For those wondering it's a sort of Simulated environment control thingy.

    Can anyone tell me what I'm missing here?
    #
    Anarion 16 years ago
    Sooo.....I'm guessing no one here knows enough about Python then to work this out. Incidentally, even my lecturer can't work out why it doesn't work.
    #
    Vacuus 16 years ago
    Please... No... Tkinter....

    Haven't used Python for a while (except in MUSHClient for scripting), but why are you using Tkinter? I know, I know, it's an easier library, but it's pretty goddamned old, ugly and ported from another, inferior language...

    Have you tried WxWidgets? Might take a little while to get into, but it's well worth the effort, I assure you.
    Otherwise, sorry, never really tried Tkinter
    #
    Anarion 16 years ago
    Well I am doing this in class. Which means you start with the very crappy basics before you get into anything better
    #
    Narvius 16 years ago
    ...is there a way to disallow the user to change console window properties?
    #
    E_net4 16 years ago
    "Cheater" said:
    ...is there a way to disallow the user to change console window properties?
    Could you be more specific?
    #
    Narvius 16 years ago
    Windows, .Net-Framework 2.0, C#, Console Application.
    Open console windows can be modified through right-clicking them on the taskbar and selecting "Properties". I want to disallow this.

    [Edit]
    I tried to set all properties in the code. Back- and Foreground colors can't be changed, but size is still editable.
    #
    Amarth 16 years ago
    I don't think that's possible. Consoles are meant to be very simple text-things, stuff like user-set colors and sizes shouldn't be relevant to what you are doing. If it is, just use the standard setting and ask users to keep that setting - if they fuck up, it's their own fault.
    #
    Narvius 16 years ago
    Oh, okay.
    I didn't think of that...
    #
    Narvius 12 years ago
    Time to engage in... Thread Necromancy! >:C

    MK: What feature do you like most in Python, and which do you consider the most unique? I'm trying to make myself learn it, but since I already know one of the two major scripting languages, I can't seem to convince myself.
    #
    E_net4 12 years ago
    Free beer if you get to impress me as well.
    #
    Narvius 12 years ago
    On an unrelated note. Guess what the output of this program is. Bonus points if you can explain wh"y".

    def wh*t
    p %w(%s)[0]%t[0]+%:es:
    end
    wh"y"

    Ville:
    The parser strips out whitespaces at the beginning (and possibly end) of a line. Justified in the latter case, but not in the former.
    #
    Amarth 12 years ago
    To be fair, Ruby is probably on the same level as Python. I think we (MK and me) just learned Python before Ruby. I like the slightly more readable Python, maybe, but it's probably personal preference by learning Python earlier and knowing more about it.

    Python vs 'non-modern languages' (Java, C, Perl, that stuff) is another case entirely of course, which E might be asking about? We could probably rant for hours about that but I'm sure we already did. "import antigravity" is probably the best argument there.

    (going on a tangent here)
    But I do think Python is slowly getting behind. The best thing it currently got going for it is IMO the huge community around it and the maturity of the main implementation and its standard libraries. As a language, I like (for example) Boo more. Note that I have the same feeling about other languages of that generation, like Ruby. Great languages, but not even close to what a language/compiler/interpreter should be able to do. Which is, mostly, prevent bugs from ever happening.(1) I know that's not possible, but there's still considerable room for improvement - there are whole classes of bugs that could be theoretically prevented with better compilers. In 20 years time, when the state of the art gets there.

    (1) other interesting goals obviously include speed of development and speed and size optimizations. But I believe that silly bugs cost the global economy and health more than improvements in other areas can help. Got to think that one through though, as this thought only just occurred to me as to why I find this so important. Hmm.
    /me goes into pondering mode
    #
    Narvius 12 years ago
    What sort of silly bugs do you deem automatically preventable?
    You see, I don't have any "real world" experience with programming, I do it (as of now) as a hobby, so I'd happily listen to your thoughts on this and / or other issues.

    (also, this Boo thing. What does it have? I found the wabside and looked into the Primer, but I figured it'd be faster to just ask you, since you know it, apparently)
    #
    Amarth 12 years ago
    Here's a perfect example: http://techcrunch.com/2008/12/31/zune-bug-explained-in-detail/

    The code is used to calculate the current date and works as follows. 'days' holds the number of days since Jan 1, 1980. To calculate the current year, you just loop (until 'days' is small enough) and add years for every 365 or 366 days. If days is, say, 400, at the start of the loop, we have to add another year, and assuming it wasn't a leap year, we're currently at day 35 (=400-365) of the next year. The bug became apparent at the last day of a leap year. At that point in time, days=366 in the final iteration of the loop, the isLeapYear(year) branch would be taken, but the if(days>366) branch would not. Of course, that means the loop would just loop without doing anything useful - it never terminates. A silly bug, but not easily seen, and not easily found by testing unless you actively search all edge cases out. Which you probably should but it's easy to miss some.

    Code analysis (the exact technicalities are kinda advanced maths - search for 'Hoare logic' as a starting point if you want to know more) can reveal that this loop has problems. The current state of research (believe me when I say I know this stuff, the university where I studied is at the spearhead of the research here) is not yet able to do this in a reasonable time for moderately complex programs however. The timeframe for this technique to be possible would be on the order of 10-20 years, adaptation in usable form for non-researchers would probably be another 5-ish years.

    And Boo is basically Python with static typing and type inference. It prevents silly bugs like trying to give a parameter of a wrong type to a function, like trying to divide two strings or something (that's obv a silly example, but you get the idea in more complex programs). Duck typing is basically the only thing I hate about Python that I think can be done better, and Boo does that for me. So yay! Targetting the CLI platform (.NET/Mono) also seems like a good idea if you can do it without sacrificing language features. Things like the GIL in Python are kinda getting silly, a decent virtual machine like CLI at least clears that up.
    #
    Narvius 12 years ago
    I see, thanks for clearing that up.
    #
    MageKing17 12 years ago
    Narvius said:
    MK: What feature do you like most in Python, and which do you consider the most unique? I'm trying to make myself learn it, but since I already know one of the two major scripting languages, I can't seem to convince myself.
    I'd say the thing I like most about Python is the fact that it took me no time at all to go from reading code to writing (and executing) my own code. Sure, it's also incredibly dynamic, and easily readable, but those things were mostly "oh, that's cool too" things for me. Mostly, it's the fact that it took me no time whatsoever to start coding the things I wanted to code.

    Granted, I no longer have any time to code in, but when I did have free time, it was coding galore!
    #
    Narvius 12 years ago
    http://tiddlywiki.com/
    This is an extremely interesting idea. A completely self-contained single-user wiki. It also looks pretty neat once you've splatted some CSS onto it.
    #
    MageKing17 12 years ago
    Narvius said:
    http://tiddlywiki.com/
    This is an extremely interesting idea. A completely self-contained single-user wiki. It also looks pretty neat once you've splatted some CSS onto it.
    ...Huh. I've been thinking about making something like that to contain various rambling ideas I've had kicking around. I was going to make it an actual wiki, though, just one with no editing privileges; but this all-on-one-page idea is pretty fascinating.

    EDIT: Wow, that thing allows fast turnaround: http://deviance.duckish.net/tw/
    #
    Narvius 12 years ago
    I've been searching for personal wikis for that very reason. But everything else required me to install a gazillion dependencies (mostly database crap), hack the commandline for hours (roughly three commands) and so on. It's not that I couldn't do it, I just couldn't be arsed.

    But Tiddlywiki is pretty neat, I can imagine it blowing up after a few hundred (at most) tiddlers though.
    #
    MageKing17 12 years ago
    Narvius said:
    But Tiddlywiki is pretty neat, I can imagine it blowing up after a few hundred (at most) tiddlers though.
    Is that an actual problem, or just your imaginings? Because if the latter, I will totally fill this thing up with hundreds of twiddlers.
    #
    Narvius 12 years ago
    Well, I think there's a reason we're used to splitting larger things up. But I didn't use it long enough yet to check if I'm right.
    #
    MageKing17 12 years ago
    Narvius said:
    Well, I think there's a reason we're used to splitting larger things up. But I didn't use it long enough yet to check if I'm right.
    Um... managability, mostly, not technical limitations. Unless you go back to the ages of, like, ZZT worlds, which could only hold so many bytes.
    #
    Narvius 12 years ago
    Oh, okay. We'll see, I guess.
    #
    Venom31 12 years ago
    And just where do you find a definition of tiddler?
    #
    Narvius 12 years ago
    A tiddler is (roughly) a page on a tiddlywiki.

    Also, MK. You didn't enable toggle links on your tw, yet you still claim that clicking a link also closes the corresponding tiddler. :3

    [Edit]
    Or wait, no. It's actually a per-user-per-tw setting, I think.
    #
    Narvius 12 years ago
    People, people, people, this is beautiful: Some pathfinding stuff.
    That can also be used for desire-driven AI stuff. And that can probably adapted into different environments than roguelikes (specifically node-based pathfinding maps, aka what Driftmoon has).
    Maybe you can use it, Ville.

    Edited 12 years ago
    #
    ville 12 years ago
    Nice stuff there. Actually the Driftmoon AI is a state machine, the AI gets into a right state, and then waits for it to finish, then looks for a new state to move into.
    #
    Narvius 12 years ago
    Clojure is awesome. With just five lines, I find the contents of a folder, filter out the .png's, load them, and then store them in a hash-map of keywords consisting of the filename without extension mapped to the loaded images themselves:

    (->> "assets" ; If I wrapped this in a function, I would substitute a foldername argument here
    (java.io.File.) (.list) (filter #(.contains % ".png"))
    (mapcat #(vector (keyword (apply str (take-while (partial not= \.) %)))
    (javax.imageio.ImageIO/read (java.io.File. (str "assets/" %)))))
    (apply hash-map))

    In purely functional style of course. None of this looping nonsense.

    Edited 12 years ago
    #
    Narvius 12 years ago
    Aaaaaaaaaaaaand...

    Generating music by redirecting program output into a sound channel.
    http://www.youtube.com/watch?v=tCRPUv8V22o
    This is... AWESOME.

    Edited 12 years ago
    #
    E_net4 12 years ago
    Hah. Using ImageIO from java to retrieve the images. That is so cheap.
    Good job, though. You seem highly capable of functional programming.


    Also, that one video... is amazing!
    #
    Narvius 12 years ago
    E_net4 said:
    Hah. Using ImageIO from java to retrieve the images. That is so cheap.
    Well, knowing your tools is just as important as general programming ability.

    This way of thinking feels pretty natural (...programistically natural, whatever that means) to me, and I greatly enjoy writing functionally. I don't know why.

    Edited 12 years ago
    #
    MageKing17 12 years ago
    Narvius said:
    Clojure is awesome. With just five lines, I find the contents of a folder, filter out the .png's, load them, and then store them in a hash-map of keywords consisting of the filename without extension mapped to the loaded images themselves:

    (->> "assets" ; If I wrapped this in a function, I would substitute a foldername argument here
    (java.io.File.) (.list) (filter #(.contains % ".png"))
    (mapcat #(vector (keyword (apply str (take-while (partial not= \.) %)))
    (javax.imageio.ImageIO/read (java.io.File. (str "assets/" %)))))
    (apply hash-map))

    In purely functional style of course. None of this looping nonsense.
    def load_images(folder):
    imagemap = {}
    for path, dirs, files in os.walk(folder):
    for file in files: imagemap[os.path.splitext(file)[0]] = pygame.image.load(os.path.join(path, file))
    return imagemap
    Technically the exact same thing, but with looping.

    (And wrapped in a function, because I am obsessively robust like that.

    And Pygame, because I can't be bothered to remember how another image-manipulating library works. )
    #
    Narvius 12 years ago
    As far as I can see you forgot about the filtering for only .png's part, but right, neat nonetheless. :3

    Also, in my case it has nothing to do with being pedantic or not; it's just a valid expression on it's own. (Which may be the case because EVERYTHING is - as long as that something exists).

    Edited 12 years ago
    #
    MageKing17 12 years ago
    Can anyone guess what this is?

        state stand (ground):
    if not __prevstate__ == "walk": anim.skip("stop")
    if not __prevstate__ == "duck": anim.skip("standup")
    on key[left] and not key[right]:
    facing = "left"
    state -> walk
    on key[right] and not key[left]:
    facing = "right"
    state -> walk
    on key[down]: state -> duck
    on key[up]: state -> charge
    on ~key[jump] and key[jump]: state -> jump
    on ~key[attack] and key[attack]: state -> attack
    on ~key[evade] and key[evade]:
    state -> backdash
    state -> superjump
    #
    Forum » General Programming thread
  • « previous
  • 1
  • 2
  • 3
  • » next
  • Post Reply


    Your email:
    Your name: