Niloofar24 15 Posting Whiz

Well, @fonzali, at the moment i tryed your code, the output was:

2015-02-24 07:21:53.769512
2015
2015-03-21 02:15:11
24 days, 18:53:17.230488
There are 67997 seconds till Persian New Year
2141597.23049

I've printed all variables in the code to see them in terminal; look at those 2 last lines. With .format(differ.seconds) the output was 67997 seconds, but with differ.total_seconds() the output was 2141597.23049. So why the results were different?! Why i've got 2 different seconds?!

Niloofar24 15 Posting Whiz

ok, i'll try them too, Thank you @Wolf 2.

Niloofar24 15 Posting Whiz

Yes, i'm familiar with the basic, PHP syntax is very easy. Thank you for explanations @diafol.

Niloofar24 15 Posting Whiz

@diafol;
I'm not new to programming in general, i'm a python programmer, but i'm new to PHP.
I understood your explanation, hope to be successful with programming the CMS i've started just today! I love to get involved with great and difficult projects even when i'm new to a language:)

Niloofar24 15 Posting Whiz

I understood, thank you @AleMonteiro.

Niloofar24 15 Posting Whiz

Hello.
I'm new to PHP, i've started it yesterday.
I want to creat a simple CMS with (PHP and PDO), what should i do?
I'm going to start with creating the CMS core. I want to create sth like a simple wordpress.
What should i do? Where should i start from? Any reference for creating CMS with PHP and PDO?

Niloofar24 15 Posting Whiz

with this:

mylist = [2,4,4]
number = int(''.join(mylist))
print number

I got this error:

    number = int(''.join(mylist))
TypeError: sequence item 0: expected string, int found
Niloofar24 15 Posting Whiz

oops sorry i waschecking another code sorry @gribouillis!!

Niloofar24 15 Posting Whiz

Thank you @vegaseat.
Could you make more clrear this line for me please?

number = int("".join(str(n) for n in mylist))

int means return ( ) into integer.
"".join means add no-space to the ( ).
I can't understand this completely str(n) for n in mylist, what does it exactly do?

-------------------------------------
@Gribouillis, with this:

mylist = [6, '/', 3, '+', 9, '+', 8, '+', 1, '/', 2]
number = int(''.join(mylist))
print number

I got this error:

    number = int(''.join(mylist))
TypeError: sequence item 0: expected string, int found
Niloofar24 15 Posting Whiz

Hello.
I have a list:

mylist = [2,4,4]

How can i take number 244 out from the list? Something like:

number = 244

Here 244 is integer.

Niloofar24 15 Posting Whiz

What do you mean by list("string")?
How can i do that with colors = "red, blue, pink"?

Niloofar24 15 Posting Whiz

@vegaseat you didn't get the error with check() because as i said, there was a def check_to_answer(): that i changed into def answer():, then i got some errors again, so i sent the complete code file.

Well, with your edited code, there is a problem.
for example if i want to add three numbers with eachother, for exampleif i press 2 then + then 3 then * then 4, all of them will be appended into a list and the result will be number 14, because the calculator will first attention to 3x4 because of the * sign and then will add 12 to number 2 and will show us number 14 as the result!

But i wanted my code to do like this:
To create a list that takes only three indexes, a first number then a sign then the second number, and check the list after every appending, to see the length of the list, if it is three, run the operation and then show the result as a label and then empty the list and append the result into the list. So if i enter 2 then + then 3, the function understand that the length of the list is three so it will calculate 2+3 ans show number 5 as the label and then empty the list and finally append number 5 into list.
Then if i press * and then 4, the function understand the length of the list is again three, …

Niloofar24 15 Posting Whiz

oops! This is really bad that we are not able to edit title or our posts! Why this forum is like that?!
I wanted to typ "creating countdown for new year begining".

Niloofar24 15 Posting Whiz

Hello friends.
I want to create a countdown program for the new year that is comming.
I know i can use this code for example, for short certain time:

from threading import Timer

def sayhi():
    print "hello everybody!"

t = Timer(10, sayhi)
t.start()

But the new year begins in 29 days (2015, march 21)(at 02:15:11 AM). So i cant calculate how many seconds is left!!

So what can i do? I think i should give the date to the program and it should use a calendar maybe, but i don't know how.

Niloofar24 15 Posting Whiz

Yes! Thank you @vegaseat. Your last code was helpful.

Niloofar24 15 Posting Whiz

@vegaseat can you explaine this part for me please?
I just don't understand something...

dq.append(item)
return list(dq)

For example we have this colors = "red, blue, pink", now if we use print (list(colors)), we get this:

['r', 'e', 'd', ',', ' ', 'b', 'l', 'u', 'e', ',', ' ', 'p', 'i', 'n', 'k']

So how the items (colors) we append into dq and return dq to a list, don't split into letters?

Niloofar24 15 Posting Whiz

Thank you @vegaseat, was helpful too.

Niloofar24 15 Posting Whiz

There was a def check_to_answer(): that i changed into def answer():.

Niloofar24 15 Posting Whiz

Here is my code:

#!/usr/bin/python2

from kivy.app import App

from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.label import Label

numbers = []

class CalculatorApp(App):
    def build(self):

        b = BoxLayout(orientation = "vertical")
        self.l = Label(text = " ")
        g = GridLayout(rows = 3)
        btn1 = Button(text = "1")
        btn2 = Button(text = "2")
        btn3 = Button(text = "3")
        btn4 = Button(text = "4")
        btn5 = Button(text = "5")
        btn6 = Button(text = "6")
        btn7 = Button(text = "7")
        btn8 = Button(text = "8")
        btn9 = Button(text = "9")
        btn10 = Button(text = "0")
        btn11 = Button(text = "+")
        btn12 = Button(text = "-")
        btn13 = Button(text = "*")
        btn14 = Button(text = "/")
        btn15 = Button(text = "=")
        btn16 = Button(text = "clear")
        btn17 = Button(text = "(")
        btn18 = Button(text = ")")

        btn1.bind(on_press = self.number_one)
        btn2.bind(on_press = self.number_two)
        btn3.bind(on_press = self.number_three)
        btn4.bind(on_press = self.number_four)
        btn5.bind(on_press = self.number_five)
        btn6.bind(on_press = self.number_six)
        btn7.bind(on_press = self.number_seven)
        btn8.bind(on_press = self.number_eight)
        btn9.bind(on_press = self.number_nine)
        btn10.bind(on_press = self.number_zero)
        btn11.bind(on_press = self.set_operator_to_add)
        btn12.bind(on_press = self.set_operator_to_sub)
        btn13.bind(on_press = self.set_operator_to_multy)
        btn14.bind(on_press = self.set_operator_to_divid)
        btn15.bind(on_press = self.answer)

        g.add_widget(btn1)
        g.add_widget(btn2)
        g.add_widget(btn3)
        g.add_widget(btn4)
        g.add_widget(btn5)
        g.add_widget(btn6)
        g.add_widget(btn7)
        g.add_widget(btn8)
        g.add_widget(btn9)
        g.add_widget(btn10)
        g.add_widget(btn11)
        g.add_widget(btn12)
        g.add_widget(btn13)
        g.add_widget(btn14)
        g.add_widget(btn15)

        b.add_widget(self.l)
        b.add_widget(g)

        return b

    def check(self):
        global numbers
        if len(numbers) == 3:
            self.answer()

    def answer(self, event):
        result = eval(" ".join(str(num) for num in numbers))
        print result
        self.l.text = str(result)
        numbers[:] = []
        numbers.append(result)


    def number_one(self, event):
        global numbers
        numbers.append(1)
        print numbers
        self.check()

    def number_two(self, event):
        global numbers
        numbers.append(2)
        print numbers
        self.check() …
Niloofar24 15 Posting Whiz

Thank you @vegaseat, i learnt something new.

But is there any way to limit a list inorder not to take a new index? I don't want the list to delete any item to make for any new item.
I want the list to only take the first 3 indexes i append, and if i append the forth number, the list refuse to take and append it. I want the list to ignore from the forth append to the end. Just to take the first 3 append numbers.

Niloofar24 15 Posting Whiz

Hi, me again :)

Look at this error please:

   File "./kivycal-1", line 103, in number_two
     self.check()
 TypeError: check() takes exactly 2 arguments (1 given)

The error is pointing to here:

    def number_two(self, event):
        global numbers
        numbers.append(2)
        print numbers
        self.check()

This is a part of a class in a .py file. I'm coding in python and kivy in that file.
And here is the check function:

    def check(self):
        global numbers
        if len(numbers) == 3:
            ckeck_to_answer()

Well, i can't understand what's the exact problem, what should i do?

Niloofar24 15 Posting Whiz

Hello again.
How can i limit my list indexes?
I want my list to take only 3 indexes for example.

Niloofar24 15 Posting Whiz

The best start for python is "Python for Kids.pdf". You can download the book from here:

http://it-ebooks.info/book/2226/

I started python with that book, it was a great tutorial with so clear explanation. Attention that it's a book for kids! So you can't find any other refrence better than this! Try it.

Niloofar24 15 Posting Whiz

Yes!! Thank you @Gribouillis.

Niloofar24 15 Posting Whiz

Just currect it:

numbers = [6, '/', 3, '+', 9, '+', 8, '+', 1, '/', 2]
Niloofar24 15 Posting Whiz

Hello.
mylist = [6, '/', 3, '+', 9, '+', 8, '+', 1, '/', 2]
How can i delete that list indexes?
I tried:

    def clear(self, event):
        global numbers
        for i in numbers:
            print i
            del numbers[i]

But it didn't work:

   File "./kivycal", line 83, in clear
     del numbers[i]
 TypeError: list indices must be integers, not str

What can i do for that?

Niloofar24 15 Posting Whiz

Yes @HiHe i know that. But the problem is that i'm trying to create a simple calculator. Every time i press a number-button, it's number will be appended to a list. And finally when i press = button, the list will go to run all math operation i have enterd their operator signs into that list. So i can't put any () into my list.

Or..... maybe i could define a method of mine for that! I will work on this part.

Niloofar24 15 Posting Whiz

And let me explain something more. in my calculator (wich is a Tkinter button) when we press a button of a number, it's number will be append into the list, then we press a operator button and the sign will be append to the list and go on. Finally when we press = button, the code i posted in my last post, will run to calculate the list and print the result.

Niloofar24 15 Posting Whiz

Thank you @Gribouillis. The code you gave was the exact thing i was looking for. Of course i will work more on parser and wisent. And also will try to develop my own method.

Now here i have a problem. Look at this:

mylist = [6, '/', 3, '+', 9, '+', 8, '+', 1, '/', 2]
result = eval(' '.join(str(x) for x in mylist))
print result

If you run this you will get 19, the result will be set to 19.
If you use your pc calculator and enter this:

6/3+9+8+1/2

And then press = button, calculator will show you number 19.5 as the result.

But if you use your pc calculator and enter like this:

6/3

And then press = button and after that enter +9 and again press = button and go on, the last number that calculator will show you will be number 10.

I know what is the reason. Calculator do operation step by step when we add numbers step to step into calculator, but when we enter all the numbers and signs togather; calculator run multiplication first, after that division, then addition and finally subtraction. So the result will be different.

Now as i'm creating a simple calculator and using the code i put above, how can i fix this problem. With the code above, my calculator will run operations with this rule */+- and the result will be number 19. How can i get the result as number 10.

I don't …

Niloofar24 15 Posting Whiz

Hello.

I have a list like this:

mylist = [7, "+", 2, "+", 1]

I want to take them out of the list inorder to run the operation and get the real result; something like this:

result = 7 + 2 + 1

So the result variable will be set to 10.
How can i do that?!

Niloofar24 15 Posting Whiz

Hello.
I have a Class in my python file. That Class contains some functions.
How can i set the command of a Tkinter butten so that call a function of that class?

I tried this but it didn't work:

class calculating(object):
    def __inint__(self):
        self.current = 0
    def addition(self, amount):
        self.current += amount
    def result(self):
        return self.current
        print (self.current)
    def show(self):
        lbl["text"] = result()


btn1 = Button(root, text = "add", command = calculating.addition)
btn1.pack()

btn2 = Button(root, text = "result", command = calculating.show)
btn2.pack()

lbl = Label(root)
lbl.pack
Niloofar24 15 Posting Whiz

I'm going to learn kivy programming language. Could you introduce some good tutorials to me please? Except the kivy.org.

And also i'm looking for a good Kivy forum.

Niloofar24 15 Posting Whiz

This a the first part of my code:

import os
import pickle

DBFILENAME = 'linknames.db'
loaded = True
choices = []

def pathtofile(filename = DBFILENAME):
    folder = os.path.dirname(os.path.abspath(__file__))
    return os.path.join(folder, filename)


def load_choices():
    global choices, loaded
    if loaded:
        return
    filepath = pathtoile()
    if not os.path.isfile(filepath):
        with open(filepath, 'wb') as fout:
            pickle.dump([], fout)

    with open(filepath, 'rb') as fin:
        choices = pickle.load(fin)
    loaded = True

def dump_choices():
    filepath = pathtofile()
    with open(filepath, 'wb') as fout:
        pickle.dump(choices, fout)

import sqlite3 as db

conn = db.connect('linkuserpass.db')
cursor = conn.cursor()
cursor.execute('CREATE TABLE IF NOT EXISTS linkuserpass(mylink TEXT, myusername TEXT, mypassword TEXT)')
print ('table is created')
conn.commit()
cursor.close()
conn.close()



def add():
    import sqlite3 as db

    link = ent1.get()
    user = ent2.get()
    password = ent3.get()

    conn = db.connect('linkuserpass.db')
    cursor = conn.cursor()
    cursor.execute('INSERT INTO linkuserpass(mylink, myusername, mypassword) VALUES(?,?,?)', (link, user, password))
    print ('Done!')
    conn.commit()
    cursor.close()
    conn.close()

    load_choices()
    choices.append(ent1.get())
    dump_choices()


def show():
    import sqlite3 as db

    key_link = var.get()

    conn = db.connect('linkuserpass.db')
    conn.row_factory = db.Row
    cursor = conn.cursor()
    cursor.execute('SELECT * FROM linkuserpass WHERE mylink=(?)' , (key_link,))
    row = cursor.fetchone()
    data_str1 = ""
    data_str2 = ""
    data_str1 = (row['myusername'])
    data_str2 = (row['mypassword'])
    print data_str1
    print data_str2
    userlabel['text'] = 'username:  ' + data_str1
    passwordlabel['text'] = data_str2
    conn.commit()
    cursor.close()
    conn.close()

And the rest is the Tkinter code part. Look at this please:

var = StringVar(root)
var.set('')

load_choices()
option = OptionMenu(root, var, choices)
option.pack()

btn2 = Button(frame5, text = 'show', command = show)
btn2.pack(side = 'left')

With load_choices that i have typed, when i run the program, no value …

Niloofar24 15 Posting Whiz

@vegaseat, look at line 29 of your code please. As you typed on the code, we need to have line 29 in our file from the beginning of running the program.
I mean i want to use load_choices for example instead of line 29. I want to load the choices list from a text file (my db file), and send it into() in lime 30.
What should i do?

Niloofar24 15 Posting Whiz

Thank you @vegaseat.

Niloofar24 15 Posting Whiz

How can i create a drop-down menu in Tkinter?

Niloofar24 15 Posting Whiz

But Tkinter is really ugly so i prefer to use Kivy, it's more beautiful than Tkinter! And also very easy to code!

Niloofar24 15 Posting Whiz

Thank you @vegaseat.

And thank you @gribouillis for the command.

And thank you all for all answers.

Niloofar24 15 Posting Whiz

Thank you @vegaseat.

I have some question again:
Look at line 24. self.label = what does the "self" word do here?

And line 29. def callback(self, event): what the "self" keyword is for? I don't know much about the SELF keyword.
And also what does the "event" keyword do here?

Thanks for this simple example, this is one way, we just have one .py file. Can you give me an example of using one .py file and one .kv file beside togather, that they are interacting togather, do you know what i mean?
If i want to create this program in that way with both .kv and .py file togather, i can't do it. The standard form of coding in kivy language is using a .py file and a .kv file togather, right?!

I would be happy if you could give me an example of that way too.
Thanks for this one, it helps.

Niloofar24 15 Posting Whiz

Well, i'm trying to learn from every where. If h find any video i watch it, i search the net and try to learn from example codes. I didn't find any special and great tutorial for that.

I checked that link but as i said, it's not clear for me how to use it exactly.
I know i should create a .py file and a .kv file, but i think that link is about only what we can use in .py file only.
I mean some tutorials use kivy in one .py file only and don't teach the standard way of kivy programming wich should use a .kv file beside the .py file and changing the .py file according to that .kv file.

Well, i don't know it was clear or not.
I nead a simple and complete example, a .py file and a .kv file to gather to see how they interact.

Niloofar24 15 Posting Whiz

I think i should use on_press keyword, but i don't know how to use it exactly. I need a simple example. For example i want when i press the button, a "Hello World" apperas on the label widget. (All in kivy language).

Niloofar24 15 Posting Whiz

Hello!
Can you give me an example of Button widget in Kivy language?
I can creat the button but don't know how to use it's callback. I mean how can i set a command for the Button in Kivy?

Niloofar24 15 Posting Whiz

And also i have a question, well, i'm confused, which media player on my OS is playing the mp3 file?!

Niloofar24 15 Posting Whiz

@vegaseat, could you please explain this code more clearly to me?! I can't understand some parts clearly. I have written comments:

import pygame as pg

#here why we should type volume=0.8 here? what does it work?
def play_music(music_file, volume=0.8):
    '''
    stream music with mixer.music module in a blocking manner
    this will stream the sound from disk while playing
    '''
    # set up the mixer
    freq = 44100 # audio CD quality
    bitsize = -16 # unsigned 16 bit
    channels = 2 # 1 is mono, 2 is stereo
    buffer = 2048 # number of samples (experiment to get best sound)

    #could you explaine this line more clear please?
    pg.mixer.init(freq, bitsize, channels, buffer)

    # volume value 0.0 to 1.0   #and also this line?
    pg.mixer.music.set_volume(volume)

    #could you please explain completely each line, one by one please? i don't understand them clearly.
    clock = pg.time.Clock()
    try:
        pg.mixer.music.load(music_file)
        print("Music file {} loaded!".format(music_file))
    except pg.error:
        print("File {} not found! ({})".format(music_file, pg.get_error()))
        return
    pg.mixer.music.play()
    while pg.mixer.music.get_busy():
        # check if playback has finished
        clock.tick(30)


#### this part is ok i understand.
music_file = "linkin_park-Until_Its_Gone-(IroMusic)-193.mp3"

# optional volume 0 to 1.0
volume = 0.8

play_music(music_file, volume)

Thank you.

Niloofar24 15 Posting Whiz

Yes!!!!!!!!!! :) It works thank you @vegaseat.

And thak you all.

Niloofar24 15 Posting Whiz

Well, i find the problem myself.
The name of the file "Tutorial.kv" should be in lowercase, so i changed it to "tutorial.kv".

And also there was a little problem in "tutorial.kv" line 6. The ; after None was extra that i deleted.

Niloofar24 15 Posting Whiz

Hello.
I'm using Linux OS, Python 2 and the last version of Kivy framework.
Here i have 2 files that i have typed them exactly as what i watched on a kivy tutorial video.
Fist file is "main.py":

from kivy.app import App

from kivy.uix.scatter import Scatter
from kivy.uix.label import Label
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.textinput import TextInput
from kivy.uix.boxlayout import BoxLayout

class ScatterTextWidget(BoxLayout):
    pass

class TutorialApp(App):
    def build(self):
        return ScatterTextWidget()


if __name__ == "__main__":
    TutorialApp().run()

And the second one is "Tutorial.kv":

<ScatterTextWidget>:
    orientation: "vertical"
    TextInput:
        id: my_textinput
        font_size: 120
        size_hint_y: None;
        height: 200
        text: "Hello World!"
    FloatLayout:
        Scatter:
            Label:
                text: my_textinput.text
                font_size: 120

Both files are on my desktop. When i run "python main.py" a black empty window appears only. Why the kivy file can't be read?! Where is the problem?

Niloofar24 15 Posting Whiz

Thank you @JasonHippy.
I'm using Linux; ubuntu.
I've installed pyglet and have copied this code from the link you gave me(just changed the name of mp3 file). And there is the error i got....

The code:

#!/usr/bin/python2
import pyglet
song = pyglet.media.load('linkin_park-Until_Its_Gone-(IroMusic)-193.mp3')
song.play()
pyglet.app.run()

The error:

Traceback (most recent call last):
  File "melody.py", line 3, in <module>
    song = pyglet.media.load('linkin_park-Until_Its_Gone-(IroMusic)-193.mp3')
  File "/usr/lib/pymodules/python2.7/pyglet/media/__init__.py", line 1386, in load
    source = _source_class(filename, file)
  File "/usr/lib/pymodules/python2.7/pyglet/media/riff.py", line 202, in __init__
    'AVbin is required to decode compressed media')
pyglet.media.riff.WAVEFormatException: AVbin is required to decode compressed media
AL lib: (EE) alc_cleanup: 1 device not closed

What's the problem? Could you give a short simple example yourself please?

Niloofar24 15 Posting Whiz

Hi.
How can i open a mp3 file and play it in python?
There is a mp3 file on my Linux desktop and i want to open it from a python file and play it.
What should i do?

Niloofar24 15 Posting Whiz

Yes i understand and i tried it but i got this error:

Traceback (most recent call last):
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1535, in __call__
    return self.func(*args)
  File "./mydb", line 45, in show
    data_str += (row['colorname']) + '\n'
UnboundLocalError: local variable 'data_str' referenced before assignment

So i asked again.

Now it works because i have typed += and also data_str = "" wich i forgot to type before and was the cause of the problem.