[Tutor] Tkinter grid question
Thank you for reading this. This is my first attempt at using Tkinter and I've quickly run into a problem. If e is a one dimensional list then all is OK and I can delete and insert entries. The problem comes about when the list is made two dimensional, as follows: from tkinter import * master = Tk() e = [None] * 6 , [None] * 2 for i in range(6): for j in range(2): e[i][j] = Entry(master, width=5) e[i][j].grid(row=i, column=j) e[i][j].insert(0,"6") mainloop( ) Traceback (most recent call last): File "/home/pi/tkinter_example.py", line 50, in e[i][j] = Entry(master, width=5) IndexError: tuple index out of range I can see that the problem occurs when i is greater than 1 which makes me think that my method of attempting to create a two denominational array of edit boxes is wrong. I've search for an example but haven't turned up anything. Where had I failed? -- Regards, Phil ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tkinter grid question
Phil wrote: > Thank you for reading this. > > This is my first attempt at using Tkinter and I've quickly run into a > problem. > > If e is a one dimensional list then all is OK and I can delete and insert > entries. The problem comes about when the list is made two dimensional, as > follows: > > from tkinter import * > > master = Tk() > > e = [None] * 6 , [None] * 2 In the above line you are creating a 2-tuple consisting of two lists: >>> [None]*6, [None]*2 ([None, None, None, None, None, None], [None, None]) What you want is a list of lists [ [None, None], [None, None], ... ] You can create such a list with >>> [[None] * 2 for _ in range(6)] [[None, None], [None, None], [None, None], [None, None], [None, None], [None, None]] > > for i in range(6): > for j in range(2): > e[i][j] = Entry(master, width=5) > e[i][j].grid(row=i, column=j) > e[i][j].insert(0,"6") > > mainloop( ) > > Traceback (most recent call last): > File "/home/pi/tkinter_example.py", line 50, in > e[i][j] = Entry(master, width=5) > IndexError: tuple index out of range > > I can see that the problem occurs when i is greater than 1 which makes me > think that my method of attempting to create a two denominational array of > edit boxes is wrong. > > I've search for an example but haven't turned up anything. Where had I > failed? As shown above this has nothing to do with tkinter. Personally I would create the list of list dynamically entries = [] for row in range(6): entries_row = [] entries.append(entries_row) for column in range(2): entry = Entry(master, width=5) entry.grid(row=row, column=column) entry.insert(0,"6") entries_row.append(entry) or even use a dict with (row, column) keys: def make_entry(row, column): entry = Entry(master, width=5) entry.grid(row=row, column=column) entry.insert(0,"6") return entry master = Tk() entries = { (r, c): make_entry(r, c) for r in range(6) for c in range(2) } ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Network Sniffing on Windows with Python 3.6
On 07/04/17 06:07, Some Developer wrote: > How would I go about writing a Python 3.6 script for Windows that would > sniff the network traffic and take the individual packets and then > reassemble them into something that is useful data? That is definitely possible using Python although it is fairly advanced networking code. Certainly further than I've ever gone using Python. > I was thinking of installing Wireshark to have a look at the network > communications but I wasn't really sure what I would do with the data. > Are there any library functions in Python 3.6 that would help with this > task? For sure, but I'd definitely install wireshark, if nothing else its likely to be near essential in debugging your code. > Also does the Python script require admin permissions for this to work > or can it run as a normal user? It doesn't matter if it does require > admin permissions but it would be better for my users if it didn't > require admin permissions. That's more likely to be a feature of the OS and who is running the code producing the data. Unless everything is running as your user account I'd suspect admin privileges will be necessary - in fact I'd hope so! > there are any books on the subject that would be even better as I like > reading books on programming subjects. My two main sources for Python networking are: Python Network Programming by Goerzen, published by APress This is a great intro to the general theory of network programming as well as the Python specifics. If you are already familiar with networking through say, the classic Stephens books on C networking, then this will be familiar ground. Its good if you want to understand what you are doing rather than just copy somebody else's code. Programming Python 4th edition. A monster book (1600 pages?) with about 500 pages dedicated to networking. It's a book within a book! A bjt less background theory, more code. If you like books this is a great value buy since it also covers GUI/Tkinter(400 pages) and Systems programming (200 pages) as well as miscellaneous other topics. -- 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] Tkinter grid question
On 07/04/17 03:09, Phil wrote: > Thank you for reading this. > > This is my first attempt at using Tkinter and I've quickly run into a problem. > Peter has already answered the problem but I'd like to point out how he used the interactive prompt >>> to demonstrate what was going wrong. You should get into the habit of always having an interactive shell running while you code, then you can instantly answer questions like this by copying code from your script into the shell and seeing the output. The shell is a vastly underused piece of pythons programming environment, its not just for beginners to learn on, it can greatly speed up your coding workflow. Anytime you wonder what a particular method does, or what a data structure looks like, just type it into the shell and find out. No more guessing. -- 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] [Python 3.5] TypeError: a bytes-like object is required, not 'str' PICKLE
Hi I have added b so that it translates into bytes object. save_cPickle part is not problem... But i still get an error when coming into load_cPickle for this function: Dataset.save_part_features('categorical_counts', Dataset.get_part_features('categorical')) although i have defined b in save_cPickle THE CODE: import _pickle as cPickle def save_cPickle(filename, data): with open(filename, 'wb') as f: cPickle.dump(data, f) def load_cPickle(filename): with open(filename) as f: return cPickle.load(f) class Dataset(object): part_types = { 'id': 'd1', 'loss': 'd1', } parts = part_types.keys() @classmethod def save_part_features(cls, part_name, features): save_cPickle('%s/%s-features.pickle' % (cache_dir, part_name), features) @classmethod def get_part_features(cls, part_name): return load_cPickle('%s/%s-features.pickle' % (cache_dir, part_name)) Dataset.save_part_features('categorical_counts', Dataset.get_part_features('categorical')) Dataset(categorical_counts=train_cat_counts).save('train') ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Network Sniffing on Windows with Python 3.6
2017-04-07 7:07 GMT+02:00 Some Developer : > Hi, > > This is more a query about where to look for information rather than > asking for specific code. > > There is a game that passes data over the network and I want to sniff the > network protocol used by the game to pass data between the client and the > server (before anyone asks no I am not cheating it is an MMO and I want to > create a database site that holds information on all the items and quests > in the game etc). > > How would I go about writing a Python 3.6 script for Windows that would > sniff the network traffic and take the individual packets and then > reassemble them into something that is useful data? I've never done > something like this before. > > I was thinking of installing Wireshark to have a look at the network > communications but I wasn't really sure what I would do with the data. Are > there any library functions in Python 3.6 that would help with this task? > > Basically I need to lock onto the games process and sniff any incomming or > outgoing network traffic from that process. Is that possible? > > Also does the Python script require admin permissions for this to work or > can it run as a normal user? It doesn't matter if it does require admin > permissions but it would be better for my users if it didn't require admin > permissions. > > I'm just looking for some help to push me in the right direction. If there > are any books on the subject that would be even better as I like reading > books on programming subjects. > > Thanks for any help :). > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > Hi, some days ago I started to investigate similar problem (check traffic) and I found the following libraries (maybe it helps You): https://pypi.python.org/pypi/pyshark_parser/0.1 https://pypi.python.org/pypi/pyshark/0.3.6.2 https://www.wireshark.org/docs/man-pages/tshark.html BR, George ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tkinter grid question
On Fri, 7 Apr 2017 10:08:40 +0100 Alan Gauld via Tutor wrote: > Peter has already answered the problem but I'd like > to point out how he used the interactive prompt >>> to > demonstrate what was going wrong. Thank you Alan. The >>> prompt, print() and Duckduckgo do get a good workout. In this case I become confused because had expected [][] to be the same as a C two dimensional array. -- Regards, Phil ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] [Python 3.5] TypeError: a bytes-like object is required, not 'str' PICKLE
Allan Tanaka via Tutor wrote: > Hi > I have added b so that it translates into bytes object. save_cPickle part > is not problem... But i still get an error when coming into load_cPickle > for this function: Dataset.save_part_features('categorical_counts', > Dataset.get_part_features('categorical')) although i have defined b in > save_cPickle > > THE CODE: > import _pickle as cPickle Why not just import pickle ? > def load_cPickle(filename): > with open(filename) as f: > > return cPickle.load(f) I've no idea what your code is supposed to do, and my attempt to run it didn't get this far -- but you have to open the file in binary mode with open(filename, "rb") as f: ... to load the pickled data. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tkinter grid question
On Fri, 07 Apr 2017 10:01:21 +0200 Peter Otten <__pete...@web.de> wrote: > > e = [None] * 6 , [None] * 2 > > In the above line you are creating a 2-tuple consisting of two lists: > > >>> [None]*6, [None]*2 > ([None, None, None, None, None, None], [None, None]) > > What you want is a list of lists > [ > [None, None], > [None, None], > ... > ] > > You can create such a list with > > >>> [[None] * 2 for _ in range(6)] > [[None, None], [None, None], [None, None], [None, None], [None, > None], [None, None]] > Thank you Peter, that makes sense. What I'm trying to do is convert a sudoku solver that I wrote using C++ and the QT library in 2005. As well as coming to grips with Tkinter I'm having trouble following my C++ code. -- Regards, Phil ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tkinter grid question
On 07/04/17 11:08, Phil wrote: > ...In this case I become confused because had expected [][] > to be the same as a C two dimensional array. It is, sort of. If you set the data up correctly to start with :-) -- 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] [Python 3.5] TypeError: a bytes-like object is required, not 'str' PICKLE
Please always send the full error trace not just the last line. The message is full of useful details which we can't currently see. On 07/04/17 09:28, Allan Tanaka via Tutor wrote: > Hi > I have added b so that it translates into bytes object. save_cPickle part is > not problem... > But i still get an error when coming into load_cPickle for this function: > Dataset.save_part_features('categorical_counts', > Dataset.get_part_features('categorical')) > although i have defined b in save_cPickle > > THE CODE: > import _pickle as cPickle > def save_cPickle(filename, data): > with open(filename, 'wb') as f: > cPickle.dump(data, f) > > > def load_cPickle(filename): > with open(filename) as f: > > return cPickle.load(f) > > class Dataset(object): > > part_types = { > 'id': 'd1', > 'loss': 'd1', > } > parts = part_types.keys() > > @classmethod > def save_part_features(cls, part_name, features): > save_cPickle('%s/%s-features.pickle' % (cache_dir, part_name), features) > > @classmethod > def get_part_features(cls, part_name): > return load_cPickle('%s/%s-features.pickle' % (cache_dir, part_name)) Does the class add anything here given it only has two class attributes and two class methods? If you aren't going to create instances then a simple module level function is probably as useful. > Dataset.save_part_features('categorical_counts', > Dataset.get_part_features('categorical')) > Dataset(categorical_counts=train_cat_counts).save('train') This tries to create an instance of Dataset but there is no matching init() method...nor is there a save() 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] want to set IDLE setting to default.
Hi, all. I have met a very annoying issue that i messed up with IDlL configuration. I changed "run" key in settings but i found it does not work and then i tried to reset it to default but i failed. Pls help me get rid of this problem because it has been bothering me for a while. Thanks. Chris ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] want to set IDLE setting to default.
On 07/04/17 23:01, Tianjiao Cui wrote: > Hi, all. I have met a very annoying issue that i messed up with IDlL > configuration. I changed "run" key in settings but i found it does not work > and then i tried to reset it to default but i failed. You need to give us more detailed descriptions of what you did. How did you change it in settings - did you change the overall key definitions to a different standard set? Did you create a bespoke key set? Did you edit the settings file directly or use the dialog? And how did you change it back? And what does "not work" mean? What happened, if anything? I'd try first of all loading a standard key set like "Classic windows" and se if those settings work. Finally, there is a dedicated IDLE mailing list which is quite helpful, so you should probably try asking there too. Its on Gmane at: gmane.comp.python.idle -- 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] Tkinter class question
Thank you for reading this. I've progressed a little further but I'm now having a problem knowing when to use the "self" reference. In the following code, the function "ckeck" is called without the need to press the "check" button. This didn't occur before I sprinkled "selfs" into the code and added "array" to the "ckeck" function. I found that I needed "self" to point "array" to my list array "e" and I think that is where the fault is. from tkinter import * class TestGUI: def __init__(self, master): self.master = master master.title("Testing") num_rows = 6 num_cols = 3 self.e = [[None] * num_cols for _ in range(num_rows)] for i in range(num_rows): for j in range(num_cols): self.e[i][j] = Entry(master, width=4, justify=CENTER, foreground="gray") self.e[i][j].grid(row=i, column=j) self.e[i][j].insert(0,"6") self.check_button = Button(master, text="Check", command=self.check(self.e)) self.check_button.grid(row=7, column=7) def check(self, array): print("checked") array[2][2].insert(0, "4") root = Tk() my_gui = TestGUI(root) root.mainloop() -- Regards, Phil ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tkinter class question - refinement
On Sat, 8 Apr 2017 02:00:38 +1000 Phil wrote: If I define "e" lists before the class then everything works as I had expected, however, I don't that's technically correct. Or is it? -- Regards, Phil ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor