Re: [Tutor] Explorer bar window(wxpython) combined to Tkinter

2017-02-08 Thread Alan Gauld via Tutor
On 08/02/17 23:09, Pooja Bhalode wrote:

> But, I want to create this in the original GUI window that I am working in
> using Tkinter. However, when I tried importing it, I am not able to
> understand how to deal with different roots,

Unfortunately you cannot mix toolkits. You either use
Tkinter or wxPython. Similarly PyQt, PyGTK, Kivy etc
all need to be used on their own.

There are a few exceptions where one toolkit is built
on another so you might be able to mix the underlying
toolkit into the higher level one, but they are rare
cases and still often don't work due to event loop clashes.

So you must either write your entire app in wxPython
or in Tkinter.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Explorer bar window(wxpython) combined to Tkinter

2017-02-08 Thread Pooja Bhalode
Hi,

I have been working on creating an explorer bar in GUI. I found the code
the explorer bar online using wx python. This creates a window shown below.
[image: Inline image 1]

But, I want to create this in the original GUI window that I am working in
using Tkinter. However, when I tried importing it, I am not able to
understand how to deal with different roots,

wxpython:
root = wx.App(False)

Tkinter
root = Tk()

The code for the wxpython is given below:
import os
import wx

class MainWindow(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)

self.panel = wx.Panel(self)
self.dir = wx.GenericDirCtrl(self.panel, size=(200, -1),
style=wx.DIRCTRL_DIR_ONLY)
self.files = wx.ListCtrl(self.panel, style=wx.LC_LIST)

self.sizer = wx.BoxSizer()
self.sizer.Add(self.dir, flag=wx.EXPAND)
self.sizer.Add(self.files, proportion=1, flag=wx.EXPAND)

self.border = wx.BoxSizer()
self.border.Add(self.sizer, 1, wx.ALL | wx.EXPAND, 5)

self.panel.SetSizerAndFit(self.border)
self.Show()

self.dir.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnSelect)


def OnSelect(self, e):
self.files.ClearAll()
list = os.listdir(self.dir.GetPath())
for a in reversed(list):
self.files.InsertStringItem(0, a)

root = wx.App(False)

win = MainWindow(None, size=(600, 400))
root.MainLoop()

whereas the code for the GUI that I am working on using Tkinter is given
below:

from Tkinter import *
import datetime
import tkMessageBox
from tkFileDialog import *
from tkMessageBox import *

root = Tk()
root.title("Design of Experiments with Parameter Estimation")
root.geometry("1000x1000")

menu = Menu(root)

root.config(menu=menu)

submenu = Menu(menu)
menu.add_cascade(label="File", menu=submenu)

submenu.add_command(label="New", command=NewWindow)
submenu.add_command(label="Open", command=OpenFile)
submenu.add_command(label="Load", command=LoadNew)
submenu.add_separator()
submenu.add_command(label="Save", command=save)
submenu.add_command(label="Save As", command=Doit)

root.mainloop()

The GUI shows as below:

[image: Inline image 2]


Can someone please tell me how to combine these two so that the
Explorer bar shows up in the space shown in the figure.

Thank you so much in advance.

Yours truly,

Pooja
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help on shoppingcart

2017-02-08 Thread Alan Gauld via Tutor
On 08/02/17 10:04, Sasiliyu Adetunji wrote:

> Pls help me on this shoppingcart  assignment

Sure. You have a very specific set of tasks.
Which bit are you stuck on?
What have you tried?

Show us your code and we can help get it working.

> Create  a class called ShoppingCart

> Create a constructor...

> Create a method add_item...

> Create a method remove_item...

> If the quantity of an item to be removed ...

> Create a method checkout ...

> Create a class called Shop 

> that has a constructor which takes no arguments...

> Make sure Shop inherits from ShoppingCart.

> In the Shop class, override the remove_item method, 


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Help on shoppingcart

2017-02-08 Thread Sasiliyu Adetunji
Hi,
Pls help me on this shoppingcart  assignment

Create  a class called ShoppingCart

Create a constructor that takes no arguments and sets the total attribute
to zero, and initializes an empty dict attribute named items.

Create a method add_item that requires item_name, quantity and price arguments.
This method should add the cost of the added items to the current value of
total. It should also add an entry to the items dict such that the key is
the item_name and the value is the quantity of the item.

Create a method remove_item that requires similar arguments as add_item. It
should remove items that have been added to the shopping cart and are not
required. This method should deduct the cost of the removed items from the
current total and also update the items dict accordingly.

If the quantity of an item to be removed exceeds the current quantity of
that item in the cart, assume that all entries of that item are to be
removed.

Create a method checkout that takes in cash_paid and returns the value of
balance from the payment. If cash_paid is not enough to cover the total,
return "Cash paid not enough".

Create a class called Shop that has a constructor which takes no arguments
and initializes an attribute called quantity at 100.

Make sure Shop inherits from ShoppingCart.

In the Shop class, override the remove_item method, such that calling
Shop's remove_item with no arguments decrements quantity by one.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Trying to use tkinter in Pycharm EDU

2017-02-08 Thread Peter Otten
Lisa Hasler Waters wrote:

> Hello,
> 
> We have been able to get the turtle module in PycharmEDU by ending our
> code with turtle.done()
> 
> However, we cannot get the tkinter module to actually run in PycharmEDU.
> For example, when we input the following very simple code we get a line in
> the console that reads  "Process finished with exit code" and we can see
> that a canvas is trying to open but it never does:
> 
> from tkinter import *
> tk = Tk()
> canvas = Canvas(tk, width=500, height=500)
> canvas.pack()
> 
> OR this code:
> 
> from tkinter import *
> tk = Tk()
> btn = Button(tk, text="click me")
> btn.pack()

Try adding the line

  tk.mainloop()


Running an event loop is necessary for every tkinter program.
 
> Both of these worked fine in IDLE. 

As idle itself uses tkinter there's probably an odd interference between 
idle and your script causing it to share the editor's event loop so that it 
appears to work.

> But we really like the Pycharm EDU and
> want to continue using it.
> 
> We are working on Macs 10.11.6 with Pycharm EDU 3.0.1.
> 
> We are obviously missing something! Any advice would be appreciated!
> 
> Thanks so much!
> 
> Lisa
> 


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Trying to use tkinter in Pycharm EDU

2017-02-08 Thread Lisa Hasler Waters
Hello,

We have been able to get the turtle module in PycharmEDU by ending our code
with turtle.done()

However, we cannot get the tkinter module to actually run in PycharmEDU.
For example, when we input the following very simple code we get a line in
the console that reads  "Process finished with exit code" and we can see
that a canvas is trying to open but it never does:

from tkinter import *
tk = Tk()
canvas = Canvas(tk, width=500, height=500)
canvas.pack()

OR this code:

from tkinter import *
tk = Tk()
btn = Button(tk, text="click me")
btn.pack()

Both of these worked fine in IDLE. But we really like the Pycharm EDU and
want to continue using it.

We are working on Macs 10.11.6 with Pycharm EDU 3.0.1.

We are obviously missing something! Any advice would be appreciated!

Thanks so much!

Lisa

-- 
Lisa Waters, PhD
Technology Integration
Middle School Coding
Lower School Digital Literacy
Flint Hill School
703.584.2300
*www.flinthill.org* 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Rock,Paper Scissors (was no subject)

2017-02-08 Thread Alan Gauld via Tutor
On 08/02/17 10:02, တာန္ခတ္သန္ wrote:
> Thanks for your answers. Your explanation makes me understand what I
> didn't understand and it discourages me to go on Python in other way
> as I feel like this doesn't suit to me. :'(

Don't be discouraged. Your central program was very nearly right and
your mistakes
are just the normal kinds of mistakes that beginners make so you are
basically
on the right track.

> Any way, can you please help and guide me to get done this. I have
> some questions:
>
> 1. Could I define player value to 2/3 values like player = 'r' 'p' 's'
> , or should  I set just any one of them.

I would just set one, the important thing is that you should always set
default
values to the same type as the real values. In this case that means a
string.
You could even use an empty string "" if you like.

> 2. For this command pc = random.choice(
> list(game_command.keys())[:3]), I don't really how to control. I don't
> want the computer picks 'e' as you said.
> Can you please  help me how to write there. I have been running a
> hundred times and it doesn't pick 'e', so I thought it works.

Yes, that is the problem, most of the time these things will work but you
cannot guarantee it. In this case, because there are only 3 values,
you could just use the values directly:

pc = random.choice( ['r','p','s'])

If you really prefer a variable, and it is better practice, then you can
change your variable definitions:

weapons = ['r','p','s']

choices = weapons + ['e']
game_command = dict(zip(choices,['Rock','Paper','Scissors','Exit']))

then use

pc = random.choice( weapons )


> 3. Is there any problem if I didn't print the player score when PC won?

The specification asked for the score to be printed:
---
For the game result the program must return details of the following:
- The user’s selection
- The computer’s selection
- The winning hand
- A running total of user wins including this game and previous games
--
It did not say to only do that if the player won...

> 4. Also how the exception to be generated.

Test the input value is valid and if not, use:

raise ValueError

The value is not valid if it is not in choices

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with Multiple Inheritance in Classes

2017-02-08 Thread Alan Gauld via Tutor
On 08/02/17 07:11, Vusa Moyo wrote:
> I have a suspicion my lecturer's question is flawed, so I'd like to pose it
> to you guys to confirm my suspicions.

I think your interpretation of the question is flawed.
See Peter's reply for why.

However another point is

>  class Cat:
>  name = ""
>  kind = "cat"
>  color = ""
>  value = 100.00
>  def description(self):
> 
> desc_str = "%s is a %s %s cat worth R%.2f." % (self.name, self.color,
> self.kind, self.value)

Python is sensitive to indentation. This line needs
to be indented inside the def statement. (This may
be a mail formatting issue but since the rest of
your code looks OK I doubt it)



-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] (no subject)

2017-02-08 Thread Alan Gauld via Tutor
On 07/02/17 21:09, တာန္ခတ္သန္ wrote:

> RockPaperScissors game using dictionary. I have written my code and it
> doesn't work at all.

So what does it do? Crash your PC? Give an error? Run silently and stop?
You need to be precise when telling us these things. If you get an error
message send us the full text.

> requirements. Can you please have a look and point out what is wrong there.

> import random
> print ("Hello.. Welcome from Rock, Paper, Scissors Game!!\n"
>+"Game instruction:\n"
>+"You will be playing with the computer.\n"
>+"You need to choose one of your choice: Rock, Paper or Scissors.\n"
>+"Enter 'e' to exit the game.\n")
> 
> game_command = {"r":"Rock","p":"Paper","s":"Scissors","e":"Exit"}
> 
> 
> score = 0
> player = 0

zero is not a valid value for player. Your code expects
a character not a number.

> try:
> while player == 'r' and player == 'p' and player == 's':

'and' in computing implies both things must be true at the same time.
player can only have one value so this test will always be false and
your loop will never run. I suspect you either want to substitute 'or'
for 'and', or use:

while player in ('r','s','p'): ...

But since you use break to exit the loop later you could
just use:

while True: ...

To force the loop to run.
This is probably the most common technique in Python
for this kind of scenario.

> pc = random.choice( list(game_command.keys())[:3])

Note that Python does not guarantee that a dictionary's values
are returned in the order you created them. So this could return
a different set of values from what you expect, eg ['r','e','p']

> player = input("\nPlease enter your choice:'r','p' or 's': ")

shouldn't you do this before starting your loop too?
Just to be sure the player wants to play? And shouldn't the
player be allowed to choose 'e' too?
Otherwise how do they know how to exit?

> print ("You selects: ", player)
> print ("PC selects:",pc)
> 
> 
> if player == pc:
> print("It's a Tie!")
> score = score
> print("Your score is: ",score)
> 
> elif player == "r":
> if pc == "p":
> print("PC win!")

shouldn't you print the score here too?

> else:
> print("You win!")
> score = score + 1
> print("Your score is: ",score)
> 
> elif player == "p":
> if pc == "s":
> print("PC win!")
> else:
> print("You win!")
> score = score + 1
> print("Your score is: ",score)
> 
> elif player == "s":
> if pc == "r":
> print("PC win!")
> else:
> print("You win!")
> score = score + 1
> print("Your score is: ",score)
> elif player == 'e' :
> break
> print("You exit the game.")
> except ValueError:
> print("\nInvalid character,try again!")

You never raise a ValueError so when would this
exception be generated?

HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with Multiple Inheritance in Classes

2017-02-08 Thread Peter Otten
Vusa Moyo wrote:

> I have a suspicion my lecturer's question is flawed, so I'd like to pose
> it to you guys to confirm my suspicions.
> 
> Here goes..
> 
> I've gone and created a Class Cat1(cat): <-- inherited class, but cant
> seem get the code right which allows the test code to run successfully.

Is the following... 

> We have a class defined for cats. Create a new cat called cat1. Set cat1
> to be a grey Burmese cat worth 3000 with the name Whiskers.

your task? Then your lecturer may be asking you to *instantiate* the Cat 
class, not to make a subclass.

>  # define the Cat class
> 
>  class Cat:
>  name = ""
> 
>  kind = "cat"
>  color = ""
>  value = 100.00

The above are all class attributes. This is unusual, as even two cates of 
the same race will have a different name and value. If I'm guessing right 
your solution will look more like

$ cat cars.py
class Car:
def __init__(self, make, color, year):
self.make = make
self.color = color
self.year = year

def __str__(self):
return "{0.color} {0.make}".format(self)

def __repr__(self):
return (
"Car(color={0.color}, "
"make={0.make}, "
"year={0.year})"
).format(self)

carpark = [
Car("Ford", "blue", 1973),
Car("Volkswagen", "yellow", 2003)
]

print(carpark)  # uses Car.__repr__
print()

print("In our car park we have")
for car in carpark:
print("- a", car)  # uses Car.__str__
$ python3 cars.py 
[Car(color=blue, make=Ford, year=1973), Car(color=yellow, make=Volkswagen, 
year=2003)]

In our car park we have
- a blue Ford
- a yellow Volkswagen
$ 

>From the view of the script a VW and a Ford work the same, so there is no 
need to introduce subclasses, but if you're asked to do it anyway here's one 
way:

$ cat cars2.py
class Car:
def __init__(self, color, year):
self.color = color
self.year = year

def __str__(self):
return "{0.color} {0.make}".format(self)

def __repr__(self):
return (
"{classname}(color={0.color}, "
"year={0.year})"
).format(self, classname=self.__class__.__name__)

class Ford(Car):
make = "Ford"

class Volkswagen(Car):
make = "VW"

carpark = [
Ford("blue", 1973),
Volkswagen("yellow", 2003)
]

print(carpark)  # uses Car.__repr__
print()

print("In our car park we have")
for car in carpark:
print("- a", car)  # uses Car.__str__
$ python3 cars2.py 
[Ford(color=blue, year=1973), Volkswagen(color=yellow, year=2003)]

In our car park we have
- a blue Ford
- a yellow VW
$ 

However, this is the kind of inheritance you will only ever see in textbook 
examples because the subclasses do not introduce differing features. As a 
rule of thumb there should be at least one method with a fundamentally 
different implementation, or one extra method that is not shared between 
baseclass and subclass.

>  def description(self):
> desc_str = "%s is a %s %s cat worth R%.2f." % (self.name, self.color,
> self.kind, self.value)
> 
>  return desc_str
> 
> # your code goes here
> 
> 
> # test code
> 
>  print(cat1.description())



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor