Niloofar24 15 Posting Whiz

No no!! wait!
Look at this:

def show():
    import sqlite3 as db

    conn = db.connect('sarah.db')
    conn.row_factory = db.Row
    cursor = conn.cursor()
    cursor.execute('SELECT * FROM colors;')
    rows = cursor.fetchall()
    for row in rows:
        print ('%s' % (row['colorname']))
        data_str = (row['colorname']) + '\n'
        lbl['text'] = data_str
    conn.commit()
    cursor.close()
    conn.close()

I think the problem is related into the "for row in rows:" part.
With the print line as we see, all colornames i have inserted to the table will be printed in the terminal!

But with the data_str = (row['colorname']) + '\n' line i have typed, i get only the last word i have added to the table.
How should i change this line?

Niloofar24 15 Posting Whiz

It seems the table has only one row, how can i add more rows each time?

Niloofar24 15 Posting Whiz

Yes,thank you @L7Sqr.

Niloofar24 15 Posting Whiz

Thank you @HiHe, it helped me.

Now there is another problem now.
I have a Tkinter window with an Entry box and add button and show button. I want to enter a color everytime and add it into table in sqlite3 and i want to print color when i click the show button.

I have created this:

cursor.execute('CREATE TABLE IF NOT EXISTS colors (colorname TEXT)')

Now i only can enter one word and insert into the table and call it again to print in the label widget, only ONE word, but i want the database to keep all words, every colorname i enter and add everytime. And i want the show button to print all colornames everytime i click it.

The problem is that it seems the table keeps only one word. I want a database to keep one kind of value, only "colorname TEXT".
How should i change the code?

Niloofar24 15 Posting Whiz

No, again with the def show(), the color will be printed in the terminal not in the label widget.

Niloofar24 15 Posting Whiz

Thank you @Gribouillis, it works.

And thank you @HiHe for your note.

Now here i have a def:

def show():
    import sqlite3 as db

    conn = db.connect('sarah.db')
    conn.row_factory = db.Row
    cursor = conn.cursor()
    cursor.execute('SELECT * FROM colors;')
    rows = cursor.fetchall()
    for row in rows:
        print ('%s' % (row['colorname']))
    conn.commit()
    cursor.close()
    conn.close()

I want now to get data from the table for printing.
With the def above, the word i have inserted into the table will be printed in the terminal, but i want to print it in a Label widget in Tkinter.
What shuld i do to print in Label widget?

Niloofar24 15 Posting Whiz

The color table has only (colorname TEXT).

And i have sent the error in my last post.

note:
I used the value "happy", i wanted to type a color but i mixed it up with another file so used the word "happy" instead of a color like "red" for example. But it's not important, anyway with any word, i don't know how to insert it into a table.

Niloofar24 15 Posting Whiz

Here is the error i get:

    cursor.execute("INSERT INTO colors VALUES (?)", (word))
ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 8 supplied.
Niloofar24 15 Posting Whiz

I searched that page and read all 'insert into" parts, so this code must be correct but why it doesn't work?

word = 'happy'

####################

cursor.execute("INSERT INTO colors VALUES (?)", (word))

I also tried this one but i doesn't work:

cursor.execute("INSERT INTO colors VALUES (%s)" % 'happy')
Niloofar24 15 Posting Whiz

Hello.

When we want to insert a vlue into a table in sqlite, we use this line, right?

cursor.execute('INSERT INTO colors Values ("red", "blue")')

As we can see above, we want to insert 2 words and we type them as when we are typing whole the codes.
But i want to get 2 words from input and insert them into the table in sqlite.
I'm using Entry box in Tkinter window and i want something like this:

ent = Entry()
ent.grid()

################

cursor.execute('INSERT INTO colors VALUES (ent.get())')

But it doesn't work and give me a syntaxt error.
What can i do?

Niloofar24 15 Posting Whiz

Hello everybody.
How can i get "ls" from shell scripting?
I tried this but it doesn't work, i saved it in a file:

#!/bin/bash
$ls

I know the second line is not correct, so what should i type?
Getting "ls" is just an example, i infact want to import this line to the shell:

sqlite3 test.db

to create a database.

Niloofar24 15 Posting Whiz

And How can i refresh an ScrollText widget?

I have a Tkinter window with an entry box and add button and refresh button and scrolltext widget.
The first time i run the program, the text appears on the scrolltext widget but when i add a word, and push the refresh button, it wont refresh the scrolltext widget with new word. why?

Niloofar24 15 Posting Whiz

Another question. (for using .pack())

I want a Label to be on the top, then an Entry box under the Label and then 3 Buttons under the Ebtry box. (3 Buttons are placed side by side).

How can i do that with using .pack()?
I have used "side = LEFT" for all of them, it put 3 Buttons side wich is ok but the Label and Entry box and 3 Buttons are all placed side by side in one line!

With using .grid() i could use row and culomn and set them but just because of the first question i have asked above, i can't use .grid() and have to use .pack().
If the first question get solved, i could use .grid() options for placing Label and Entry box and Buttons.

Niloofar24 15 Posting Whiz

Hi everybody.

I have problem with .pack() and .grid().

I have to use this line:

    mywin.pack(fill=BOTH, expand=YES)

I have used .grid every where on my file but now here i have copied a pice of code from some where and have pasted to my file, as you see it has used .pack().
I can't change all .grid() to .pack() because it's difficult for me to use .pack() when i want to set for example the place and direction of buttons, so i use .grid() options like row and column.

And if i change that line, that .pack() to .grid(), it doesn't work.
So what should i do?

(Thats the reason i don't like Tkinter at all, it's not friendly.)

Niloofar24 15 Posting Whiz

I wanted to start new discussion for this question but i will ask here.

How can i set the size of the ScrollText widget?
I have an entry box and some buttons on my Tkinter window and i want the ScrolledText widget to be placed don the right side of the main window; how can i set a border fot that widget?
Well i don't know if my question was clear or not so let me give you a link:
http://stackoverflow.com/questions/17657212/how-to-code-the-tkinter-scrolledtext-module

On that page you can see an image of a little window taht has a ScrolledText widget on it with borders. You can see green borders. I want something like that. AS you see, that ScrolledText widget has not fixed whole the window, it lets us to see tha main window color wich is green.

Hope you understand what i mean.
I tried to use the code which was on that page but it didn't wok for me and left some errors.

Niloofar24 15 Posting Whiz

Hello.
I have used ScrolledText in my code, the scroll bar that appears is attached to the frame and although it scrolls the text box contents, I want that it was attached to the text widget itself.
What can i do?

Niloofar24 15 Posting Whiz

@HiHe, what do you mean by "with conn:"?
Can you explain it more clear please?

Niloofar24 15 Posting Whiz

Thank you @Gribouillis.

Niloofar24 15 Posting Whiz

@Gribouillis, do i have to add these 3 lines into the end of the "dbcreate.py"?

conn.commit()
cursor.close()
conn.close()

Is it necessary? I ask it because i see the file will works ok even without those 3 lines.
"dbcreate.py" file is just for creating database and we don't set any value on this file, so no need to commit, right? What about closing cursor and conn on this file?
I know i should type those 3 lines in all files but myquestion is just about "dbcreate.py" file.

Niloofar24 15 Posting Whiz

Oh yes! Thank you @Gribouillis. It works now. The man didn't type that on the video tutorial.

Niloofar24 15 Posting Whiz

After that, I did this one in "dbquery.py" file:

import sqlite3 as db

conn = db.connect('test.db')
conn.row_factory = db.Row
cursor = conn.cursor()

cursor.execute("select * from films")
rows = cursor.fetchall()
for row in rows:
    print ("%s %s %s" % (row["title"], row["year"], row["director"]))
conn.close()

But it did'nt have any result again.
Why? Anyone can test my codes on Linux to find the problem?

Niloofar24 15 Posting Whiz

I tried SELECT but there was no changes.
First i created db with "dbcreate.py" file:

import sqlite3 as db

conn = db.connect('test.db')
cursor = conn.cursor()
cursor.execute("create table films (title text, year text, director text)")
print ("table created")

Then i inserted vlues on db with "dbinsert.py" file:

import sqlite3 as db

conn = db.connect('test.db')
cursor = conn.cursor()
cursor.execute('insert into films values ("matrix","1999","The Wachowski Brothers")')
cursor.execute('insert into films values ("V for Vendetta","2005","James McTeigue")')
conn.close()

After that when i typed "sqlite3 test.db" and then typed "select * from films" nothing appeared.

Niloofar24 15 Posting Whiz

Hi everybody.
I'm using python to create sql database. I created a test.db file and then made a table on it with the name "fims" and inserted some data on it.... title text, year text and director text. And then set values for them.

Now when i type "sqlite3 test.db" on the shell, and the "secelt * from films;" nothing apears.
I did exactly what i saw on the tutorial video but the result was different.

(Of course the tutorial is using windows, but i use Linux, so i know i should change some commands a little a bit, but abut this command, i don't know what to do)

Niloofar24 15 Posting Whiz

I fixed the problem myself. Thank you.

Niloofar24 15 Posting Whiz

My code file hadn't these errors before.

Niloofar24 15 Posting Whiz

Well, now when i type "./app3" on the terminal, i get this error:

./app3: line 6: import: command not found
./app3: line 7: import: command not found
./app3: line 9: DBFILENAME: command not found
./app3: line 10: loaded: command not found
./app3: line 11: favorite_movies: command not found
./app3: line 14: syntax error near unexpected token `('
./app3: line 14: `def pathtofile(filename=DBFILENAME):'

But why? For example line 6 is import os and line 7 is import pickle.

Niloofar24 15 Posting Whiz

Hello everybody.
I need a quick help.
I have a problem with running my python file code.
I use Linux. I had a file with the name "app3.py" file on the Desktop; then i removed ".py" from the end of the name of the file; so now i have "app3" file. The first line of the file is this:

#!/usr/bin/env python3

I have checked the "Execute: Allow executing file as program" checkbox too.
I have typed this command on Linux terminal:

sudo chmod +x app3

Now when i type "./app3" on the terminal; it should works but it doesn't. I don't know why.
What should i do? I need a quick help.
Thank you

Niloofar24 15 Posting Whiz

Hello friends :)
I'm almost familiar with Tkinter and Kivy frameworks, but i'm looking for other python GUI frameworks, soething better than Tkinter. Kivy is also great but i need to watch more tutorial videos and i'm not able to do that right now. Can you introduce a better and more easier framework to me?
Thank you ;)

Niloofar24 15 Posting Whiz

This is an example code:

#!/usr/bin/env python3
# -*-coding: utf8-*-

from Tkinter import *

root = Tk()
photo = PhotoImage(file = "bg.gif")
w = Label(root, image=photo)
w.pack()

ent = Entry(root)
ent.pack()
ent.focus_set() 

root.mainloop()

With the code above; a window appears with an image background; but the entry box will be places out of that background, under the image. But i want the entry box to be places on the middle of the photo area.

We set the window background with this for example:

root.configure(background = 'AntiqueWhite1')

I want to set an image instead of a color.

Niloofar24 15 Posting Whiz

Hello.
How can i set an image as a Tkinter window background? I mean the whole background of the window not a photo area.

Niloofar24 15 Posting Whiz

This is a simple example:

from Tkinter import ttk

window = Ttk()
button = ttk.Button(window, text='hi')
button.pack()
window.mainloop()

Ande the error:

Traceback (most recent call last):
  File "ttk.py", line 1, in <module>
    from Tkinter import ttk
ImportError: cannot import name ttk
Niloofar24 15 Posting Whiz

Hello.
Imagin we have a python file with many alphabet letters like this: (Which is infact meaningful like i am a programmer)

i
a
m
a
p
r
o
g
r
a
m
m
e
r

How can i print them randomly that nobody could read it? sth like this for example:

g
a
i
m
r
a
m
r
p
r
a
o
e
m

And i want the program when i enter the password, print the exact primary model which we had first.

Please try to guide me first, friends. I dont't want the main answer now because i'm trying to learn and want to try and get the correct result myself, i just need your guidness first.
If i couldn't get the correct result, i will ask you to help me with anser finally.
Thank you so much :)

Niloofar24 15 Posting Whiz

I fixed the problem. Thank you.

Niloofar24 15 Posting Whiz

Hi friends!
I'm coding a program; i have a Tkinter window with a view button and i can add names with entry box and add button into a list that will be saved on a db file. Now i want the program to show the list names otomaticly when the program runs. The only problem is that if i want to run the program for the first time, there is no db file, so nothing can be load, so the program won't run and i get this error:

Traceback (most recent call last):
  File "app3.py", line 156, in <module>
    refresh()
  File "app3.py", line 75, in refresh
    refresh()
  File "app3.py", line 64, in refresh
    favorite_movies = pickle.load( open("films.db", "rb"))
IOError: [Errno 2] No such file or directory: 'films.db'

It's almost a long file code so i can't post them here.

Before i decided the program to show the list name otomaticly at the same time as the program runs, when i run the program for the first time, it always created the db file itself, but now with changing the code, i don't know what to do.

Niloofar24 15 Posting Whiz

Thank you @woooee. It helped.

Niloofar24 15 Posting Whiz

Can you explaine a little more clear?! My language is Persian, what do you want to do? I did'nt understand your meaning.

Niloofar24 15 Posting Whiz

Hello!
My this time question is about breaking a list.
I have a list with the name favorite_movies. Here is 2 functions of all functions from the file:

def add():
    load_favorite()
    favorite_movies.append(ent.get())
    dump_favorite()
    #print (colors)




def view():
    load_favorite()
    from Tkinter import *

    v = Tk()

    v.geometry('300x300')
    v.configure(background = 'white')

    top = Label(v)
    top.pack()

    top.configure(text = favorite_movies)

    v.mainloop()
    dump_favorite()

When i append a name with the Add button and then press the View button, i see list's names on the Tkinter window like this: spiderman lastnight matrix

but i want them to be displayed like this:
spiderman
lastnight
matrix

What should i do?

Niloofar24 15 Posting Whiz

Thanks @Gribouillis, it works.

Niloofar24 15 Posting Whiz

Oopss yes sorry, i posted an other error.
This is the exact error:

Traceback (most recent call last):
  File "real.py", line 18, in <module>
    btn = Tkinter.Button(window, text = 'add', command = callback()).pack()
  File "real.py", line 6, in callback
    lbl.configure(text = 'button clicked!')
AttributeError: 'NoneType' object has no attribute 'configure'
Niloofar24 15 Posting Whiz

Hi everybody.
How can i install ttk module on linux?
When i try to import and use it on my Tkinter window, the error says sth like there is no module named ttk. So it means i should install it right?! I'm using python 2. I want to use ttk to make buttons more nicely.

Niloofar24 15 Posting Whiz

Yes you are right, i should post the error too.
Here is the error:

Traceback (most recent call last):
  File "real.py", line 19, in <module>
    btn = Tkinter.Button(window, text = 'add', command = callback()).pack()
  File "real.py", line 7, in callback
    Label['text'] = 'Good morning'
NameError: global name 'Label' is not defined

Well i'm using python 2.

Niloofar24 15 Posting Whiz

Hello friends :)
This is my code:

#!/usr/bin/env python3
# -*-coding: utf8-*-


def callback():
    lbl.configure(text = 'button clicked!')


import Tkinter

window = Tkinter.Tk()
window.geometry('300x300')
window.configure(background = 'PeachPuff3')
lbl = Tkinter.Label(window, text = 'Nothing yet!')
btn = Tkinter.Button(window, text = 'add', command = callback).pack()

window.mainloop()

Why it doesn't work?

Niloofar24 15 Posting Whiz

Thank you so much, were clear comments :) Thank you.

Niloofar24 15 Posting Whiz

Well, with the code above, the names will be displayed on terminal window.
But i want when i type python favemovies.py view the view function run and the names of the favorite_movies display on a Tkinter pop up window.
I dont know how to do that.

Niloofar24 15 Posting Whiz

Sorry, was long part of codes but i just want to learn. I love programming specially in Python :)

Niloofar24 15 Posting Whiz

@Gribouillis, i didnt understand some details, whould you mind explaining each of them for me, please?

I will put the parts i didn't understand clearly:
Thank you :)

#!/usr/bin/env python3
# -*-coding: utf8-*-
import os
import pickle

DBFILENAME = 'films.db'
loaded = False          #What does this line do?
favorite_movies = []

#can you explain each line of this function please?
def pathtofile(filename=DBFILENAME):
    """Return absolute path to the db file"""
    # use db file in the same folder as
    # our program (this could be different)
    folder = os.path.dirname(os.path.abspath(__file__))
    return os.path.join(folder, filename)

#And can you explain each line of this function too please? 
def load_favorite():
    global favorite_movies, loaded
    if loaded:
        return
    filepath = pathtofile()
    if not os.path.isfile(filepath): # <- file does not exist
        with open(filepath, 'wb') as fout:
            pickle.dump([], fout) # <- dump an empty fav movies list in file

    # The 'with' construct ensures that the file is
    # properly closed after the with block
    with open(filepath, 'rb') as fin:
        favorite_movies = pickle.load(fin)
    loaded = True

#And also each line of this function too please? 
#What does FOUNT word do here? Is it a keyword?
def dump_favorite():
    filepath = pathtofile()
    with open(filepath, 'wb') as fout:
        pickle.dump(favorite_movies, fout)
Niloofar24 15 Posting Whiz

SO, what should i do now?! I didn't understand what should i do exactly.

Niloofar24 15 Posting Whiz

One more question.
I have changed the Good morning sentence with print (favorite_movies), it doesn't work i know its not correct, so how can i call this function and print the list on pop up winow? I also used the Tkinter code you gave me and repladed the message() part with view().

def view():
    favorite_movies = pickle.load( open("films.db", "rb"))
    print (favorite_movies)
    label['text'] = print (favorite_movies)
Niloofar24 15 Posting Whiz

Yes it works @sneekula, thank you so much for your help :)

Niloofar24 15 Posting Whiz

Yesssssssssssss!!! It works :)
@Gribouillis, Thank you so much.