Joseph Harari
Joseph Harari

Reputation: 17

Printing the results of a function into a tkinter message box?

I've been coding for years and I have always been able to find an answer on google, until now. I cannot manage to make this work in any way.

Before this I have several lists of different countries, and a dozen or so functions that just print stuff for the user, it is not relevant here.

I use tkinter to create an input box where a user can type a country (input gets assigned to typedCountry). I then search for the country in each and every list in the mainProgram() function, and every time I find a list that matches I return another function. Everything works marvelously as it should, except I want mainProgram() to return the information to a tkinter GUI box and not the terminal. I've been fighting with it for hours and I cannot find a way to make it work, I am willing to take any advice, even changing the code significantly or using something other than tkinter would work just fine.

def mainProgram():
    typedCountry = e.get()
    Country = typedCountry.lower()
    print 'Your country is: ' + typedCountry + '\n'
    if Country in bannedCountries:
        banned(typedCountry)
    if Country in cpBannedCountries:
        cpBanned(typedCountry)
    if Country in skrillBannedCountries:
        skrillBanned(typedCountry)
    if Country in bacsCountries:
        Bacs(typedCountry)
    if Country in sepaCountries:
        sepa(typedCountry)
    if Country in eftCountries:
        eft(typedCountry)
    if Country in ltdCountries:
        ltd(typedCountry)
    if Country in marketsCountries:
        markets(typedCountry)

master = Tk()
e = Entry(master)
e.pack()
e.focus_set()
var = mainProgram()

def textBox():
    root = Tk()
    label = Message(root, textvariable=var)
    label.pack()
    root.mainloop()

b = Button(master, text = "Search", command = mainProgram)
b.pack()

mainloop()

And here is the main code if you want it (if you want to run this in your end for example):

from tkinter import *
import tkMessageBox



bannedCountries = ['afghanistan','american samoa','belarus','brazil','burundi',
                    'central african republic','congo','cook islands','cote d\'ivoire',
                    'crimea','cuba','guam','iran','japan','liberia','libya','myanmar',
                    'new zealand','north korea','northern mariana islands','puerto rico',
                    'russia','singapore','somalia','south korea','sudan','syria','tokelau',
                    'turkey','ukraine','united states','vanuatu','virgin islands',
                    'western sahara','zimbabwe']

cpBannedCountries = ['belarus','bosnia and herzegovina','burundi','cote d\'ivoire',
'cuba','iran','iraq','kosovo','lebanon','liberia','macedonia','montenegro','myanmar',
'nigeria','north korea','saint helena','somalia','sudan']

skrillBannedCountries = ['angola','barbados','benin','burkina faso','cape verde',
'comoros','djibouti','faroe islands','gambia','greenland','grenada','guyana','laos',
'liechtenstein','macao','martinique','mongolia','namibia','niger','palau','samoa',
'suriname','tajikistan','togo','trinidad and tobago','turkmenistan']

bacsCountries = ["united kingdom"]

eftCountries = ['australia']

sepaCountries = ['austria','belgium','bulgaria','cyprus','czech republic','check',
'denmark','estonia','finland','france','germany','greece','hungary','iceland',
'ireland','italy','latvia','liechtenstein','lithuania','luxembourg','malta',
'martinique','mayotte','monaco','netherlands','norway','poland','portugal',
'romania','slovakia','slovenia','spain','sweden','switzerland','united kingdom']

ltdCountries = ['austria','belgium','bulgaria','croatia','cyprus','czech republic',
'denmark','estonia','finland','france','germany','greece','hungary','ireland',
'italy','latvia','lithuania','luxembourg','malta','netherlands','poland','portugal',
'romania','slovakia','slovenia','spain','united kingdom']

marketsCountries = ['albania','algeria','andorra','angola','anguilla','armenia',
'aruba','bahamas','bangladesh','barbados','belize','benin','bermuda','bhutan',
'bonaire','bosnia','herzegovina','bosnia and herzegovina','botswana','brunei',
'burkina faso','burma','cambodia','cameroon','cape verde','cayman islands',
'chad','comoros','djibouti','equatorial guinea','eritrea','ethiopia','falkland islands (malvinas)',
'faroe islands','gabon','gambia','ghana','greenland','grenada','guinea','guinea-bissau',
'guyana','haiti','iceland','india','jamaica','jordan','kazakhstan','kenya',
'kiribati','kosovo','kyrgyzstan','laos','lesotho','liechtenstein','macao',
'macedonia','madagascar','malawi','malaysia','maldives','mali','marshall islands',
'mauritania','mauritius','micronesia','mongolia','morocco','mozambique','namibia',
'nauru','nepal','niger','nigeria','norway','pakistan','palau','papua new guinea',
'philippines','rwanda','saint helena','saint kitts and nevis','saint lucia','saint vincent and the grenadines',
'samoa','sao tome and principe','senegal','serbia','seychelles','sierra leone',
'solomon islands','sri lanka','suriname','swaziland','tajikistan','tanzania','togo',
'tonga','trinidad and tobago','tunisia','turkmenistan','turks and caicos islands','tuvalu',
'uganda','uzbekistan','yemen','zambia']

def banned(x):
    if 'kingdom' not in x:
        return 'Clients from %s are not able to open an account with FXCM' % x
    else:
        return

def cpBanned(x):
    return "FXCM does not accept cards issued in %s" % x

def skrillBanned(x):
    return "Clients from %s cannot use Skrill" % x

def Bacs(x):
    return """Clients from %s can use BACS if their bank account is located in
%s and both their bank account and their FXCM account is in GBP""" % (x, x)

def sepa(x):
    return """Clients from %s can use SEPA if their bank account is located either in
%s or in another European country, and both their bank account and their FXCM account is in EUR""" % (x, x)

def eft(x):
    return """Clients from %s can use EFT if their bank account is located in
%s, and both their bank account and their FXCM account is in AUD""" % (x, x)
    print "Clients from %s must open with FXCM AU" % x

def ltd(x):
    return "Clients from %s must open with FXCM LTD" % x

def markets(x):
    return "Clients from %s must open with FXCM Markets" % x

def mainProgram():
    typedCountry = e.get() # This is the text you may want to use later
    Country = typedCountry.lower()
    print 'Your country is: ' + typedCountry + '\n'
    if Country in bannedCountries:
        banned(typedCountry)
    if Country in cpBannedCountries:
        cpBanned(typedCountry)
    if Country in skrillBannedCountries:
        skrillBanned(typedCountry)
    if Country in bacsCountries:
        Bacs(typedCountry)
    if Country in sepaCountries:
        sepa(typedCountry)
    if Country in eftCountries:
        eft(typedCountry)
    if Country in ltdCountries:
        ltd(typedCountry)
    if Country in marketsCountries:
        markets(typedCountry)

master = Tk()
e = Entry(master)
e.pack()
e.focus_set()
var = mainProgram()

def textBox():
    root = Tk()
    label = Message(root, textvariable=var)
    label.pack()
    root.mainloop()

b = Button(master, text = "Search", command = mainProgram)
b.pack()

mainloop()

Upvotes: 1

Views: 1926

Answers (3)

Andres Moreno Abrego
Andres Moreno Abrego

Reputation: 65

# First create a text box
txt = scrolledtext.ScrolledText(root)
# this line is for deleting it's content
txt.delete(1.0, END)
# this other line is for inserting text in it
txt.insert(1.0, 'Some Text')

Upvotes: 0

user4171906
user4171906

Reputation:

You don't call the textBox function. For this to work, if I understand the problem correctly, the called function has to update the text box label. Also, you don't send the lower() county to the function. A shortened version of your code

import sys
if sys.version_info[0] < 3:
    import Tkinter as tk     ## Python 2.x
else:
    import tkinter as tk     ## Python 3.x

ltdCountries = ['austria','belgium','bulgaria','croatia','cyprus','czech republic']
sepaCountries = ['austria','belgium','bulgaria','cyprus','czech republic','check']
marketsCountries = ['albania','algeria','andorra','angola','anguilla']

def ltd(country):
    var.set(var.get() +" *** ltd " + country)

def sepa(country):
    var.set(var.get() +" *** sepa " + country)

def markets(country):
    var.set(var.get() +" *** markets " + country)

def mainProgram():
    typedCountry = e.get()
    print('Your country is: ' + typedCountry + '\n')
    country_lower=typedCountry.lower()
    for country_list, country_function in ((ltdCountries, ltd),
                                           (sepaCountries, sepa),
                                           (marketsCountries, markets)):
        if country_lower in country_list:
            country_function(country_lower)

master = tk.Tk()
e = tk.Entry(master)
e.pack()
e.focus_set()

var=tk.StringVar()
var.set("")
tk.Label(master, textvariable=var, bg="lightyellow",
         width=25).pack()

b = tk.Button(master, text = "Search", command = mainProgram)
b.pack()
tk.Button(master, text="Quit", bg="orange",
          command=master.quit).pack()
master.mainloop()

Upvotes: 0

Nae
Nae

Reputation: 15335

Just replace for example:

return "FXCM does not accept cards issued in %s" % x

with:

Label(master, text="FXCM does not accept cards issued in %s" % x).pack()

in each of your functions.


Or better add:

lbl = Label(master)
lbl.pack()

under your e lines and then replace the returns with:

lbl['text'] = x

Upvotes: 1

Related Questions