Re: [Tutor] tkinter code executes before function returned
On 15/04/18 18:10, Alan Gauld via Tutor wrote: On 15/04/18 03:57, Chris Roy-Smith wrote: I am trying to get tkinter to return a number from a window, which then sets how many times to print a sign. I don;t jhave time to look at this in detail just now, maybe later. But first impressions is that you have a very unorthodox style of Tkinter programming. Its more traditional to build the entire GUI up front rather than creating and destroying widgets each time you execute an event handler. Its less disturbing to the user than having things appear/disappear etc, as you seem to be doing. You can make widget hide/show/deactivate themselves without destroying them just by withdrawing/unpacking them etc or changing their status, if that's really what you want to do. The code does not wait till the function returns a value, resulting in the signcount variable in having a None value, giving an output like below. I'll look at this a bit more closely later if nobody else answers by then... This is where you call your function. Looking at it quickly I think you would be as well using the standard Tkinter simpledialogs/messagebox modules to get user input. Have you looked at the simpledialogs? Thank you Alan, I didn't know of simpledialogs. That was all I needed to search documentation for these. I have now achieved what I was trying to do, with user interface as I was planning. Or better still having a static entry field on your GUI and just reading that? I'll have to figure out how to achieve that! Perhaps my intended design is not in line with modern styles? Regards, Chris Roy-Smith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] XML Programs
Hi, Maybe you should give a try to xmltodict package George leam hall ezt írta (időpont: 2018. ápr. 16., H 15:14): > Yeah, understood. > > Okay, knowing that others are smarter about python, and ElementTree, > here's some code I was using to parse XML. Took a while to recover > from. :) > > Leam > > # > > import xml.etree.ElementTree as ET > import os > import argparse > import fnmatch > > def show_info(file, element): > action = "" > net_proto = "" > trans_proto = "" > r_port = "" > l_port = "" > direction = "" > name= "" > has_info= False > f_name = "" > > id= element.attrib['name'] > f_name= os.path.splitext(file)[0] > > for setting in element.iter('Setting'): > if setting.attrib['name'] == 'Action': > action= setting.attrib['value'] > has_info = True > elif setting.attrib['name'] == '+NetworkProtocol#0': > net_proto = setting.attrib['value'] > has_info = True > elif setting.attrib['name'] == '+TransportProtocol#0': > trans_proto = setting.attrib['value'] > has_info= True > elif setting.attrib['name'] == '+RemotePort#0': > r_port= setting.attrib['value'] > has_info = True > elif setting.attrib['name'] == '+LocalPort#0': > l_port= setting.attrib['value'] > has_info = True > elif setting.attrib['name'] == 'Direction': > direction = setting.attrib['value'] > has_info = True > elif setting.attrib['name'] == 'Name': > name = setting.attrib['value'] > has_info = True > > if has_info: > outfile.write("%s ; %s ; %s ; %s ; %s ; %s ; %s ; %s ; %s\n" % > (f_name, id, name, action, net_proto, trans_proto, l_port, r_port, > direction)) > > > > ## Main > parser = argparse.ArgumentParser() > parser.add_argument("-o", "--outfile", default = "new_out.csv", > help="File to write to.") > parser.add_argument("-d", "--dir", default = ".", help="Directory of > the XML files.") > args = parser.parse_args() > > indir = args.dir > outfile = open(args.outfile, 'w') > outfile.write("File ;Rule ID ;Name ;Action ; Network Protocol; > Transport Protocol; Local Port; Remote Port; Direction\n") > > for file in os.listdir(indir): > if fnmatch.fnmatch(file, '*.xml'): > full_file = indir + "\\" + file > tree = ET.parse(full_file) > root = tree.getroot() > for element in root.iter('PolicySettings'): > show_info(file, element) > > outfile.close() > > > > On Mon, Apr 16, 2018 at 9:07 AM, Glen wrote: > > I understand, I'd love to use something else but the save game files are > in > > XML so I have no choice :'( > > > > On 16 April 2018 at 13:54, leam hall wrote: > >> > >> On Mon, Apr 16, 2018 at 7:10 AM, Glen wrote: > >> > Hey guys, > >> > > >> > I'm writing a save-game editor for a game I play (just a project to > >> > learn). > >> > But I am struggling on how to structure the code, how to store the xml > >> > data > >> > in data structure etc, > >> > > >> > Can anyone recommend some source I can review that reads and writes > data > >> > from an xml file. > >> > >> A friend's comment was "life is too short for XML". I like that. Have > >> you considered JSON? Taking it a step further, MongoDB (JSON) or > >> SQLite (SQL)? Both are pretty common and standard. > >> > >> While Python has its own stuff, like Pickle, that means you can only > >> use Python. Using something like JSON or SQL means others can use the > >> data and you get a chace to develop in a shared environment. :) > >> ___ > >> Tutor maillist - Tutor@python.org > >> To unsubscribe or change subscription options: > >> https://mail.python.org/mailman/listinfo/tutor > > > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] XML Programs
Yeah, understood. Okay, knowing that others are smarter about python, and ElementTree, here's some code I was using to parse XML. Took a while to recover from. :) Leam # import xml.etree.ElementTree as ET import os import argparse import fnmatch def show_info(file, element): action = "" net_proto = "" trans_proto = "" r_port = "" l_port = "" direction = "" name= "" has_info= False f_name = "" id= element.attrib['name'] f_name= os.path.splitext(file)[0] for setting in element.iter('Setting'): if setting.attrib['name'] == 'Action': action= setting.attrib['value'] has_info = True elif setting.attrib['name'] == '+NetworkProtocol#0': net_proto = setting.attrib['value'] has_info = True elif setting.attrib['name'] == '+TransportProtocol#0': trans_proto = setting.attrib['value'] has_info= True elif setting.attrib['name'] == '+RemotePort#0': r_port= setting.attrib['value'] has_info = True elif setting.attrib['name'] == '+LocalPort#0': l_port= setting.attrib['value'] has_info = True elif setting.attrib['name'] == 'Direction': direction = setting.attrib['value'] has_info = True elif setting.attrib['name'] == 'Name': name = setting.attrib['value'] has_info = True if has_info: outfile.write("%s ; %s ; %s ; %s ; %s ; %s ; %s ; %s ; %s\n" % (f_name, id, name, action, net_proto, trans_proto, l_port, r_port, direction)) ## Main parser = argparse.ArgumentParser() parser.add_argument("-o", "--outfile", default = "new_out.csv", help="File to write to.") parser.add_argument("-d", "--dir", default = ".", help="Directory of the XML files.") args = parser.parse_args() indir = args.dir outfile = open(args.outfile, 'w') outfile.write("File ;Rule ID ;Name ;Action ; Network Protocol; Transport Protocol; Local Port; Remote Port; Direction\n") for file in os.listdir(indir): if fnmatch.fnmatch(file, '*.xml'): full_file = indir + "\\" + file tree = ET.parse(full_file) root = tree.getroot() for element in root.iter('PolicySettings'): show_info(file, element) outfile.close() On Mon, Apr 16, 2018 at 9:07 AM, Glen wrote: > I understand, I'd love to use something else but the save game files are in > XML so I have no choice :'( > > On 16 April 2018 at 13:54, leam hall wrote: >> >> On Mon, Apr 16, 2018 at 7:10 AM, Glen wrote: >> > Hey guys, >> > >> > I'm writing a save-game editor for a game I play (just a project to >> > learn). >> > But I am struggling on how to structure the code, how to store the xml >> > data >> > in data structure etc, >> > >> > Can anyone recommend some source I can review that reads and writes data >> > from an xml file. >> >> A friend's comment was "life is too short for XML". I like that. Have >> you considered JSON? Taking it a step further, MongoDB (JSON) or >> SQLite (SQL)? Both are pretty common and standard. >> >> While Python has its own stuff, like Pickle, that means you can only >> use Python. Using something like JSON or SQL means others can use the >> data and you get a chace to develop in a shared environment. :) >> ___ >> Tutor maillist - Tutor@python.org >> To unsubscribe or change subscription options: >> https://mail.python.org/mailman/listinfo/tutor > > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] XML Programs
I understand, I'd love to use something else but the save game files are in XML so I have no choice :'( On 16 April 2018 at 13:54, leam hall wrote: > On Mon, Apr 16, 2018 at 7:10 AM, Glen wrote: > > Hey guys, > > > > I'm writing a save-game editor for a game I play (just a project to > learn). > > But I am struggling on how to structure the code, how to store the xml > data > > in data structure etc, > > > > Can anyone recommend some source I can review that reads and writes data > > from an xml file. > > A friend's comment was "life is too short for XML". I like that. Have > you considered JSON? Taking it a step further, MongoDB (JSON) or > SQLite (SQL)? Both are pretty common and standard. > > While Python has its own stuff, like Pickle, that means you can only > use Python. Using something like JSON or SQL means others can use the > data and you get a chace to develop in a shared environment. :) > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] XML Programs
On Mon, Apr 16, 2018 at 7:10 AM, Glen wrote: > Hey guys, > > I'm writing a save-game editor for a game I play (just a project to learn). > But I am struggling on how to structure the code, how to store the xml data > in data structure etc, > > Can anyone recommend some source I can review that reads and writes data > from an xml file. A friend's comment was "life is too short for XML". I like that. Have you considered JSON? Taking it a step further, MongoDB (JSON) or SQLite (SQL)? Both are pretty common and standard. While Python has its own stuff, like Pickle, that means you can only use Python. Using something like JSON or SQL means others can use the data and you get a chace to develop in a shared environment. :) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] XML Programs
Hey guys, I'm writing a save-game editor for a game I play (just a project to learn). But I am struggling on how to structure the code, how to store the xml data in data structure etc, Can anyone recommend some source I can review that reads and writes data from an xml file. Thanks, ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Need help please
On 16/04/18 03:30, Sandra Sherif via Tutor wrote: > Dear Python Tutor, > > I am in desperate need for help with programming on python. I am new to using > python and I’m trying to write a program called “turtle tag”. I’m trying to > do these things in the program: > a. Asks how many turtles are playing tag > b. Creates a turtle, assigns it a random color, and assigns it a random > starting > position in the graphical window > c. Randomly assigns one of the turtles to be “it”, and marking that turtle in > some > way (like making it a special color no other turtle is allowed to be) > d. Figures out which turtle is the closest to the turtle that is “it” > e. Moves the turtle that is it to a point where it is touching the closest > other turtle > > Can you please show me how it should be written please? Thank you so much. When writing almost any program it helps to divide it into chunks that you know how to do. You can then build each chunk separate and gradually combine them to form the final complete program. Your problem has already been divided into chunks: a-e. So which bits of that can you do? And which bits puzzle you? a) Do you know how to get a number from a user? b1) Can you create a turtle? b2) Can you choose a random color value? b3) Can you set a turtles color? b4) Can you choose a random starting position? b5) Can you set a turtles position? c1) Can you create several turtles? c2) Can you store them in a list? c3) Can you choose a random list element? c4) can you store a reference to a list element? c5) Can you choose a color that no other turtle can take? (This may mean changing the way b2 works) d1) Can you work out the distance between two turtles? d2) Can you repeat that for each turtle in your list? e) Can you move a turtle to a new location? For each item that you know how to do write a small function (do you know how to write functions?) named after the action it performs. For example: getNumber(), createTurtle(), getRandomColor(), setColor(turtle, color) etc... Test each function. Remember that functions that get a value will need to have that value stored in a variable. Write another function that uses these function to complete each major step (a-e) above. eg initializeTurtle() could cover steps a and b. For the steps you don't know how to do, or those that don't work, come back here for help. Show us your code plus any error messages(in full) Hint: It may make life easier if you forget about the random aspects for now and get it working with fixed values - easier to debug... Once it works with fixed values introduce randomness bit by bit - say colors first then position. Divide and conquer is the key. 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
[Tutor] Need help please
Dear Python Tutor, I am in desperate need for help with programming on python. I am new to using python and I’m trying to write a program called “turtle tag”. I’m trying to do these things in the program: a. Asks how many turtles are playing tag b. Creates a turtle, assigns it a random color, and assigns it a random starting position in the graphical window c. Randomly assigns one of the turtles to be “it”, and marking that turtle in some way (like making it a special color no other turtle is allowed to be) d. Figures out which turtle is the closest to the turtle that is “it” e. Moves the turtle that is it to a point where it is touching the closest other turtle Can you please show me how it should be written please? Thank you so much. ~ Sandra Loza ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor