Car park cost calculator python 2.7 Tkinter - python-2.7

Im very new to python and im currently trying to create gui for a program that calculates the cost of parking. When running the code there are no errors but the program doesn't produce a label with the calculated cost. I also want it to produce a message asking for a valid input code if the letters C,D or V are not entered into the entry boxes. Any help in where im going wrong would be appreciated! I know the layout doesn't look good at the moment i'm going to go back and fix the placement of labels afterwards its more getting core basics of the code working that i'm struggling with.
-- coding: cp1252 --
import Tkinter as tk
import re
from functools import partial
def Vehicle(label_result, n1, n2):
Vehicle = Vehicletypeentrytext.get()
Vehicle_upper = Vehicle.upper()
if not re.match("^[C,V,D]*$", Vehicle_upper):
label_result.config(text="Please input valid vehicle code")
return
elif Vehicle_upper=='C':
def carcost(label_result, n1, n2):
num1 = (n1.get())
num2 = (n2.get())
result = (int(num2)-int(num1))+1
label_result.config(text="Cost for Parking is %d" % result)
return
elif Vehicle_upper=='V':
def vancost(label_result, n1, n2):
num1 = (n1.get())
num2 = (n2.get())
result = (int(num1)-int(num2))+2
label_result.config(text="Cost for Parking is%d" % result)
return
elif Vehicle_upper=='D':
def disabledcost(label_result, n1, n2):
num1 = (n1.get())
num2 = (n2.get())
result = (int(num1)-int(num2))
label_result.config(text="Cost for Parking is%d" % result)
return
root = tk.Tk()
root.geometry('800x600')
root.title('Simple Calculator')
number1 = tk.StringVar()
number2 = tk.StringVar()
Title = tk.Label(root, text="SHORTSTAY CARPARK PAYMENT").grid(row=0, column=2)
Price = tk.Label(root, text = "1 hour = $2.00 / $3.00 / $1.00\n2 hour = $3.00 / $4.00 / $2.00\n3 hour = $4.00 / $5.00 / $3.00\n4 hour = $5.00 / $6.00 / $4.00\n").grid(row=1, column=0)
Vehicletypeentrytext = tk.StringVar()
Vehicletypelabel = tk.Label(root, text = 'Enter your Vehicle type:\nC for Car\tV for Van\tD for Disabled driver').grid(row=2, column=3)
Vehicletypeentry = tk.Entry(root, textvariable=Vehicletypeentrytext).grid(row=4, column=3)
labelNum1 = tk.Label(root, text="Enter Time in:").grid(row=5, column=0)
labelNum2 = tk.Label(root, text="Enter Time Out:").grid(row=6, column=0)
entryNum1 = tk.Entry(root, textvariable=number1).grid(row=5, column=2)
entryNum2 = tk.Entry(root, textvariable=number2).grid(row=6, column=2)
labelResult = tk.Label(root)
labelResult.grid(row=7, column=2)
Vehicle = partial(Vehicle, labelResult, number1, number2)
buttonCal = tk.Button(root, text="Calculate", command=Vehicle).grid(row=7, column=0)
root.mainloop()

You were calling functions inside def Vehicle(label_result, n1, n2) which isn't adding anything to your script so it preventing the result to display as a Label.No need to create function inside your if elif statement.check my edit on your function def Vehicle(label_result, n1, n2)
import Tkinter as tk
import re
from functools import partial
def Vehicle(label_result, n1, n2):
Vehicle = Vehicletypeentrytext.get()
Vehicle_upper = Vehicle.upper()
if not re.match("^[C,V,D]*$", Vehicle_upper):
label_result.config(text="Please input valid vehicle code")
return
elif Vehicle_upper == 'C':
num1 = (n1.get())
num2 = (n2.get())
result = (int(num2) - int(num1)) + 1
label_result.config(text="Cost for Parking is %d" % result)
return
elif Vehicle_upper == 'V':
num1 = (n1.get())
num2 = (n2.get())
result = (int(num1) - int(num2)) + 2
label_result.config(text="Cost for Parking is%d" % result)
return
elif Vehicle_upper == 'D':
num1 = (n1.get())
num2 = (n2.get())
result = (int(num1) - int(num2))
label_result.config(text="Cost for Parking is%d" % result)
return
root = tk.Tk()
root.geometry('800x600')
root.title('Simple Calculator')
number1 = tk.StringVar()
number2 = tk.StringVar()
Title = tk.Label(root, text="SHORTSTAY CARPARK PAYMENT").grid(row=0, column=2)
Price = tk.Label(root, text = "1 hour = $2.00 / $3.00 / $1.00\n2 hour = $3.00 / $4.00 / $2.00\n3 hour = $4.00 / $5.00 / $3.00\n4 hour = $5.00 / $6.00 / $4.00\n").grid(row=1, column=0)
Vehicletypeentrytext = tk.StringVar()
Vehicletypelabel = tk.Label(root, text = 'Enter your Vehicle type:\nC for Car\tV for Van\tD for Disabled driver').grid(row=2, column=3)
Vehicletypeentry = tk.Entry(root, textvariable=Vehicletypeentrytext).grid(row=4, column=3)
labelNum1 = tk.Label(root, text="Enter Time in:").grid(row=5, column=0)
labelNum2 = tk.Label(root, text="Enter Time Out:").grid(row=6, column=0)
entryNum1 = tk.Entry(root, textvariable=number1).grid(row=5, column=2)
entryNum2 = tk.Entry(root, textvariable=number2).grid(row=6, column=2)
labelResult = tk.Label(root)
labelResult.grid(row=7, column=2)
Vehicle = partial(Vehicle, labelResult, number1, number2)
buttonCal = tk.Button(root, text="Calculate", command=Vehicle).grid(row=7,
column=0)
root.mainloop()

Related

How to return calculations to a template for all objects in a class model in Django

I am trying to build a simple form which calculates which machine would run a film width the quickest, the parameters and capabilities of each machine are held in a django model.
The width of the film and how much of it will be entered in the form and the quantity needed. The function should work out which machine(s) can run it, what the max speed is and the average speed over the machines that are capable.
I want to return the values of the calculation and maybe run a for loop and display the values for each machine in a results.html template in a table. I also want to display average times across machines capable of running the widths of film.
I had some success with lists but would like to use a class that I can use in the template and do away with the lists.
Any help with this would be much appreciated as I am getting pretty lost in it!
I have only started on the 'Layflat Tubing' function in the hope that I can get it right and just copy down to the other functions.
from django.views.generic.base import TemplateView
from django.shortcuts import render
import math, datetime
from settings.models import Extruder
class Result:
def __init__(self, ext_no, width, speed=0, ):
self.ext_no = ext_no
self.width = width
self.speed = speed
def __str__(self):
return self.ext_no
extruders = Extruder.objects.all()
class FilmSpeedView(TemplateView):
template_name = 'calculations/film-speed.html'
class BagWeightView(TemplateView):
template_name = 'calculations/bag-weight.html'
class CalculatorsView(TemplateView):
template_name = 'calculations/calculators.html'
def result(request):
film_type=''
film_width=''
measure=''
speeds = [0]
quantity = 0
max_speed = 0
ave_speed = 0
ave_time = 0
max_time = 0
a=[]
b=[]
c=[]
d=[]
e=[]
if request.method=="GET":
film_type = str(request.GET["film_type"])
film_width = int(request.GET["film_width"])
edge_trim = int(request.GET["edge_trim"])
quantity =int(request.GET["quantity"])
measure = str(request.GET["measure"])
if measure == "metric":
film_width = int(film_width)
else:
film_width = film_width * 25.4
if edge_trim is None:
edge_trim = 0
else:
edge_trim = int(edge_trim)
if str(film_type) == 'Layflat Tubing':
film_type = "LFT"
for extruder in extruders:
bur = film_width / extruder.die_size
if film_width < extruder.min_width:
b.append(extruder.name + ' : Film too narrow')
extruder = Result(ext_no = extruder.ext_no, width = 'too narrow')
elif film_width > extruder.max_width:
b.append(extruder.name + ' : Film too wide')
extruder = Result(ext_no = extruder.ext_no, width = 'too wide')
else:
percentage = film_width / extruder.max_width
speed = extruder.max_kgs_hr * percentage
extruder = Result(ext_no = extruder.ext_no, speed = round(extruder.max_kgs_hr * percentage, 2), width = 'ok')
speeds.append(speed)
max_speed = max(speeds)
ave_speed = sum(speeds) / len(speeds)
ave_time = float(quantity) / ave_speed * 60.0
max_time = float(quantity) / max_speed * 60.0
else:
film_type = "Invalid Film Type"
m = a
n = b
o = c
g = str(round(ave_speed, 2)) + 'kg\'s/h'
h = str(datetime.timedelta(minutes=ave_time))
i = str(datetime.timedelta(minutes=30))
j = str(round(max_speed, 2)) + 'kg\'s/h'
k = str(datetime.timedelta(minutes=max_time))
return render(request, 'calculations/result.html', {'a':a, 'b':b, 'c':c, 'd':d, 'e':e, 'g':g, 'h':h, 'i':i, 'j':j, 'k':k, 'm':m, 'n':n, 'o':o, 'bur':bur,})

boost.python : pandas dataframe to c++

I want to use boost.python to use multi-index columns dataframe in c++.
※multi-index columns dataframe is like
I changed the type of multi-index columns dataframe into csv.
My csv file looks like this on spreadsheet
The reason why I want to use this data is for backtest. This is my backtest code in python that I want to translate to c++.
import pandas as pd
import numpy as np
from utils import load_data, load_list_csv, to_int
class No_Strategy():
def __init__(self, codes, unit, cash, position):
self.codes = codes
self.unit = unit
self.cash = cash
self.buy_signal = [0]*len(codes)
self.sell_signal = [0]*len(codes)
self.valid = 0
self.position = position
self.pass_st = 0 # 전략에 들어가지도 못한 경우
def set_data(self, prev_fs_row, fs_row, indi_row):
self.prev_fs = prev_fs_row
self.fs = fs_row # multi dimensional df
self.indi = indi_row
def _strat(self, prev_fs, curr_fs, curr_indi):
curr_rev = prev_rev = curr_ni = prev_ni = ni_growth = curr_asset = noncurr_asset = curr_asset_rat = 0
try:
prev_rev = int(prev_fs['매출액'].replace(",",""))
curr_rev = int(curr_fs['매출액'].replace(",",""))
except:
self.pass_st += 1
return 0, 0
rev_growth=(curr_rev-prev_rev)/prev_rev
try:
prev_ni = int(prev_fs['당기순이익'].replace(",",""))
curr_ni = int(curr_fs['당기순이익'].replace(",",""))
except:
self.pass_st += 1
return 0, 0
ni_growth=(curr_ni-prev_ni)/prev_ni
try:
curr_asset = int(curr_fs['유동자산'].replace(",",""))
noncurr_asset = int(curr_fs['비유동자산'].replace(",",""))
except:
self.pass_st += 1
return 0, 0
curr_asset_rat = curr_asset / noncurr_asset
#### this is the buy strategy! You can change the below ####
if (curr_indi.golden_cross) or (curr_indi.rsi_k < 0.65) :
return 1, 0
#### ************************************************** ####
#### this is the sell strategy! You can change the below ####
if (curr_indi.dead_cross):
return 0, 1
#### ************************************************** ####
return 0, 0
def run(self):
for i, code in enumerate(self.codes):
self.valid = 0
prev_fs = self.prev_fs[code]
curr_fs = self.fs[code]
curr_indi = self.indi[code]
prev_fs_cell = None
curr_fs_cell = None
try:
prev_fs_cell = prev_fs.iloc[0].replace(",","")
try:
curr_fs_cell = curr_fs.iloc[0].replace(",","")
except:
self.pass_st += 1
pass
except:
self.pass_st += 1
pass
if (curr_fs_cell != None) & (prev_fs_cell != None):
self.valid = 1
buy, sell = self._strat(prev_fs, curr_fs, curr_indi)
if self.valid == 0:
self.pass_st += 1
continue
else: # buy or sell signal get
price = curr_indi['close']
if buy:
if self.cash >= self.unit * price:
self.buy_signal[i] = self.unit
self.position[i] += self.unit
self.cash -= price * self.unit
elif sell:
if self.position[i] > 0 :
sell_num = self.position[i] - int(self.position[i]/2)
self.sell_signal[i] = sell_num
self.position[i] = int(self.position[i]/2) # 1-> 1 sell, 4 -> 2 sell ....
self.cash += price * sell_num
##title
class Broker():
def __init__(self, codes):
self.cash = 200000000 #2억
self.cash_df = None #pd.DataFrame(columns=['cash'])
self.position = [0]*len(codes)
self.position_df = None #pd.DataFrame(columns=codes) # for accumulated profit calculation
self.buy_signal = None #pd.DataFrame(columns=codes) # codes = KOSPI_stock_names
self.sell_signal = None #pd.DataFrame(columns=codes)
self.codes = codes # 012934, 3281, ...
self.unit = 1 # 주식 매매 단위
self.pass_st = 0
def set_strat(self, strategy):
self.strategy = strategy # class
def set_time(self, time_index): # time_index type: pd.Index / time range for indi df
self.buy_signal = pd.DataFrame(columns = self.codes, index = time_index) #set_index(time_index)
self.sell_signal = pd.DataFrame(columns = self.codes, index = time_index) #.set_index(time_index)
self.position_df = pd.DataFrame(columns = self.codes, index = time_index)
self.cash_df = pd.DataFrame(columns = ['cash'], index = time_index)#.set_index(time_index)
self.time_index = time_index
def set_data(self, fs, indi, price):
self.fs = fs # multi dimensional df / start: 0th - nth
self.indi = indi # multi dimensional df / start : 1th - nth
self.price = price # 2 dimensional (date X codes : close price)
def update_data(self, strategy, date):
self.cash = strategy.cash
self.cash_df.loc[date] = strategy.cash
self.position = strategy.position
self.position_df.loc[date] = strategy.position #list
self.buy_signal.loc[date] = strategy.buy_signal #list
self.sell_signal.loc[date] = strategy.sell_signal #list
self.pass_st += strategy.pass_st
def run(self):
for date in self.time_index: #아마 수정해야 할 확률 높음
if date.year == 2021:
break
else:
prev_fs_row = self.fs.loc[date.year-1] # ex: 2014
fs_row = self.fs.loc[date.year] # 2015
indi_row = self.indi.loc[date] # 2015
strategy = self.strategy(self.codes, self.unit, self.cash, self.position)
strategy.set_data(prev_fs_row, fs_row, indi_row)
strategy.run()
self.update_data(strategy, date)
def performance(self):
# !!!! 2020년까지의 결과만 성능 평가 ####
cash_df = self.cash_df[self.cash_df.index < '2021']
position_df = self.position_df[self.position_df.index < '2021']
price = self.price[self.price.index < '2021']
buy_signal = self.buy_signal[self.buy_signal.index < '2021']
sell_signal = self.sell_signal[self.sell_signal.index < '2021']
last_price = price.iloc[-1]
total_remain_num = self.position # last(2020) position data
total_buy = (price * buy_signal).sum(axis=1).sum()
total_sell = (price * sell_signal).sum(axis=1).sum()
total_remain = (last_price * total_remain_num).sum()
print(f'remain 개수: {total_remain_num}, total_remain: {total_remain} total_buy: {total_buy}, total_sell={total_sell}')
profit = total_sell + total_remain - total_buy
try:
return_mean = profit / total_buy
except:
print("no buy")
return
accum_df = (cash_df['cash'] + ((price.fillna(0) * position_df).sum(axis=1))).to_frame() # row sum
daily_return_df = (accum_df - accum_df.shift(1))/accum_df.shift(1)-1
SSE = ((daily_return_df - return_mean)**2).sum().item()
std = np.sqrt(SSE/(accum_df.shape[0]-1)) # route(sigma(x-x_bar)^2 / (n-1))
sharp = return_mean / std
self.return_mean = return_mean
self.sharp = sharp
print(f'return_mean: {return_mean}, sharp: {sharp}')
code_path = GDRIVE_DATA_PATH + 'codes.csv'
fs_path = GDRIVE_DATA_PATH + 'fs_total.csv'
indi_path = GDRIVE_DATA_PATH + 'indi_total.csv'
price_path = GDRIVE_DATA_PATH + 'prices.csv'
fs_total = load_data("fs_total.csv")
indi_total = load_data("indi_total.csv") # stock price and indicator(Golden cross, RSI, etc.)
prices = load_data("prices.csv") # stock close price data rows:date, cols: stock code.
time_index = indi_total.index # time index of indi_total multi-index columns
broker = Broker(codes)
broker.set_strat(No_Strategy)
broker.set_time(time_index)
broker.set_data(fs_total, indi_total, prices)
broker.run()
broker.performance()
I want to translate it not changing much in flow of the code.
But I cannot find how to get multi-index columns dataframe in c++, and transfer its row data to No_Strategy to decide whether invest into the stock.
※ I uploaded similar question before and get thankful answer, but it is too complicated for me so I question one more time with detail information.
look at https://github.com/hosseinmoein/DataFrame. It has about 95% of Pandas functionality in a much faster framework

How do I get my code to repeat itself if the desired input is not entered?

I just started python 2 weeks ago and I don't know how to make my code repeat itself when the input is something like "fox" when that is not one of the three options I'm accepting (horse, oxen, mule). Also if I want to total up the cost of 2 horse and say 3 mules by having it ask "do you want to buy any more animals", how would i do that? Any help would be very appreciated.
zoo = {}
def main():
print ("Animals available to purchase: " + "horse: 50, oxen: 35, mule: 20")
total_money = 1000
print ("You have " + str(total_money) + " to spend")
cost(total_money)
def animal_cost(total_money):
animal_type = raw_input("Please enter an animal:")
print ("Animal Type Entered: " + str(animal_type))
global animal_quantity
animal_quantity = int(raw_input("Please enter quantity of animals:"))
print ("Animal Quantity Entered: " + str(animal_quantity))
if animal_type in zoo:
zoo[animal_type] += animal_quantity
else: zoo[animal_type] = animal_quantity
while True:
if animal_type == 'horse':
return 50 * animal_quantity
if animal_type == 'oxen':
return 35 * animal_quantity
if animal_type == 'mule':
return 20 * animal_quantity
else:
cost(total_money)
def cost(total_money):
costing = animal_cost(total_money)
total_money -= costing
if total_money <= 0:
print ("No money left, resetting money to 1000.")
total_money = 1000
zoo.clear()
print ("Cost of Animals: " + str(costing))
print ("Remaining Balance:" + str(total_money))
choice = raw_input("Do you want to buy any more animals?(Y/N):")
if choice in('Y','y'):
cost(total_money)
elif choice in ('N','n'):
choice_2 = raw_input("Enter 'zoo' to see the animals you have purchased:")
if choice_2 in('zoo','Zoo'):
print zoo
choice_3 = raw_input("is everything correct?(Y/N):")
if choice_3 in ('Y','y'):
print ("Thank you for shopping!")
elif choice in ('N','n'):
print ("Restarting Transaction")
zoo.clear()
cost(total_money)
if __name__ == '__main__':
main()
You may try this enhanced version of your code:
zoo = {} # creating global dictionary for adding all animals into it.
# function to get animal cost
def animal_cost(total_money):
animal_type = raw_input("Please enter an animal:")
print ("Animal Type Entered: " + str(animal_type))
animal_quantity = int(raw_input("Please enter quantity of animals:"))
print ("Animal Quantity Entered: " + str(animal_quantity))
if animal_type in zoo:
zoo[animal_type] += animal_quantity
else: zoo[animal_type] = animal_quantity
if animal_type == 'horse':
return 50 * animal_quantity
if animal_type == 'oxen':
return 35 * animal_quantity
if animal_type == 'mule':
return 20 * animal_quantity
# getting total_money after animal purchase
def cost(total_money):
costing = animal_cost(total_money)
total_money = total_money - costing
if total_money <=0: # condition when money is less than or equal to 0.
print("No Money left, resetting money to 1000.")
total_money = 1000
print ("Cost of Animals:" + str(costing))
print ("Total Money Left:" + str(total_money))
# Recursion for buying more animals:
choice = raw_input("Do you want to buy any more animals?:")
if choice in ('Yes','y','Y','yes','YES'):
cost(total_money)
# you can use this commented elif condition if you want.
else: # elif choice in('no','No','n','N','NO'):
print("thankyou!!")
print("You have total animals: "+str(zoo))
# main function to initiate program
def main():
print ("Animals available to purchase: " + "horse, oxen, mule")
total_money = 1000
print ("You have " + str(total_money) + " to spend")
cost(total_money)
if __name__ == '__main__':
main()
This might help you out.
Have a look into this for last two lines

If loop in tkinter - button pressed

How to get tkinter to enter the if loop when button pressed to print? I do not want the print command to be in the button function.
Code:
from tkinter import *
class Player:
N = 0
def __init__(self, name, points):
# instance variables goes here
self.name = name
self.score = points
# increment class variable
Player.N = Player.N+1
class G_501:
def __init__(self, players=[]):
self.NoPlayers = len(players)
print("Number of players",self.NoPlayers)
Spillere = []
Score = []
Runde = 0
for i in range(0, len(players)):
P = Player(players[i], 501)
print("Player", Player.N, P.name, "- Point:", P.score)
Spillere.append(P.name)
Score.append(P.score)
Score = list(map(int, Score))
root = Tk()
c = [False]
def half_bull(c, event=None):
c.append(True)
return c
b1 = Button(root, text="Half Bull", bg="green", fg="black", command=lambda c=c: half_bull(c))
b1.pack()
print(c)
if c[-1] == True:
print("Fedt")
root.mainloop()
S = G_501(["Chr"])
Only need help with the tkinter part... Thanks!

Attribute error occurs when I run my GUI code in Python. Python gives me no information as to what the error is or what is causing it

This is my code and when i try to run it and use it by entering numbers into the entry fields, an attribute error occurs when i try to call on the entry variable. This is the message that appears:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1486, in call
return self.func(*args)
File "F:\HomeWork\Yr13\Extended Project\Notepad++\Python27\Programs\GUI{c} menu+check+gen+Nth.py", line 224, in OnGenButtonClick
n= str(Prime_Generation(Prime_Gen.entryVariable5.get(),Prime_Gen.entryVariable6.get()))
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1845, in getattr
return getattr(self.tk, attr)
AttributeError: entryVariable5
any help would be greatly appreciated.
i have stuck on this problem for three days and have tried getting around the problem by using different functions and names and the error still occurs
import Tkinter
from Tkinter import *
import math
def SoS(limit):
numbers = range(3, limit+1, 2)
half = (limit)//2
initial = 4
for step in xrange(3, limit+1, 2):
for i in xrange(initial, half, step):
numbers[i-1] = 0
initial += 2*(step+1)
if initial > half:
plist = [2] + filter(None, numbers)
return plist
break
def S(m):
sieve = [True] * m
for i in xrange(3,(int(m**0.5)+1),2):
if sieve[i]:
sieve[i*i::2*i]=[False]*((m-i*i-1)/(2*i)+1)
plist = [2] + [i for i in xrange(3,m,2) if sieve[i]]
return plist
def OTS(n):
n, correction = n-n%6+6, 2-(n%6>1)
sieve = [True] * (n/3)
for i in xrange(1,(int(n**0.5)/3)+1):
if sieve[i]:
k=3*i+1|1
sieve[ k*k/3 ::2*k] = [False] * ((n/6-k*k/6-1)/k+1)
sieve[k*(k-2*(i&1)+4)/3::2*k] = [False] * ((n/6-k*(k-2*(i&1)+4)/6-1)/k+1)
plist = [2,3] + [3*i+1|1 for i in xrange(1,n/3-correction) if sieve[i]]
return plist
def is_prime(num):
if num <= 3:
if num <= 1:
return False
return True
if not num % 2 or not num % 3:
return False
for i in xrange(5, int(num ** 0.5) + 1, 6):
if not num % i or not num % (i + 2):
return False
return True
def is_prime_multiple(Lower,Upper):
NumberList = dict()
if Lower%2 == 1:
for i in xrange(Lower, Upper,2):
NumberList[i] = is_prime(i)
else:
for i in xrange(Lower-1,Upper,2):
NumberList[i] = is_prime(i)
NumberList[1] = False
return [i for i in NumberList if NumberList[i] == True]
def Prime_Generation(L,U):
Lower = int(L)
Upper = int(U)
if Lower == 1:
if Upper < 92:
print SoS(Upper)
if Upper >= 92 and Upper < 2250:
print S(Upper)
if Upper >= 2250 :
print OTS(Upper)
else:
print sorted(is_prime_multiple(Lower,Upper))
def factors(n):
f = 3
fs = []
while n % 2 == 0:
fs.append(2)
n /= 2
while f*f <= n:
while n % f == 0:
fs.append(f)
n /= f
f += 2
if n > 1:
fs.append(n)
return fs
def Prime_Checker(N):
NStr = str(N)
N = int(N)
Nfs = factors(N)
for i in Nfs:
if i != N:
NfsStr = str(Nfs).strip('[]')
resultb = [NStr,' is not a prime. The prime factors of ',NStr,' are ',NfsStr]
return resultb.join()
else:
return N,' is a prime. The prime factors of ',N ,' are ',N
def PrimeFinderLamda(n,limit):
nums = range(3,limit,2)
for i in range(2, int(limit**0.5)):
nums = filter(lambda x: x == i or x % i, nums)
return [2]+nums
def NthPrime(N):
N = int(N)
Lower = 1
limit = N*N
if N == 1:
return 2
else:
return PrimeFinderLamda(N,limit)[N-1]
class Prime_app_tk(Tkinter.Tk):
def __init__(self,parent):
Tk.__init__(self,parent)
self.parent = parent
self.initialize()
def Prime_Gen_Win(self):
Prime_Gen = Toplevel()
Prime_Gen.grid()
Prime_Gen.labelVariable3 = StringVar()
Title_label2 = Label(Prime_Gen,textvariable=Prime_Gen.labelVariable3,
relief = RAISED,fg="black",bg="white"
,font = "Arial")
Title_label2.grid(column=0,row=0,columnspan=4)
Prime_Gen.labelVariable3.set(u"Please enter the upper and lower limits of the prime number generation")
Prime_Gen.labelVariable4 = StringVar()
SubTitle_label1 = Label(Prime_Gen,textvariable=Prime_Gen.labelVariable4,fg="black",bg="white")
SubTitle_label1.grid(column=0,row=1,columnspan=4)
Prime_Gen.labelVariable4.set(u"(Please enter values no greater than 10 million)")
Prime_Gen.entryVariable5 = StringVar()
Prime_Gen.entry = Entry(Prime_Gen,textvariable=Prime_Gen.entryVariable5)
Prime_Gen.entry.grid(column=0,row=4)
Prime_Gen.entryVariable5.set(u"Lower.")
Prime_Gen.entryVariable6 = StringVar()
Prime_Gen.entry = Entry(Prime_Gen,textvariable=Prime_Gen.entryVariable6)
Prime_Gen.entry.grid(column=0,row=5)
Prime_Gen.entryVariable6.set(u"Upper.")
Genbutton = Button(Prime_Gen,text=u"Generate !",command=self.OnGenButtonClick #placing and aesthetics of button
,bg="yellow",relief=RAISED,padx=10,pady=10
,activebackground="red",activeforeground="white")
Genbutton.grid(column=0,row=6)
scrollbar = Scrollbar(Prime_Gen)
scrollbar.grid(column=1,row=8,sticky="ns")
Prime_Gen.Result_label = Text(Prime_Gen, yscrollcommand=scrollbar.set
,fg="blue",bg="white",wrap=WORD
,width=100,relief = SUNKEN)
Prime_Gen.Result_label.grid(column=0,row=8,columnspan=2)
scrollbar.config(command=Prime_Gen.Result_label.yview)
Prime_Gen.labelVariable = StringVar()
SubTitle_label = Label(Prime_Gen,textvariable=Prime_Gen.labelVariable,fg="black",bg="white")
SubTitle_label.grid(column=0,row=9,columnspan=4)
Prime_Gen.labelVariable.set(u"To see full list please click on the results\n and use the up and down arrows to scroll through the list")
Prime_Gen.grid_columnconfigure(0,weight=1)
Prime_Gen.resizable(True,True)
Prime_Gen.update()
Prime_Gen.geometry(Prime_Gen.geometry())
Prime_Gen.entry.focus_set()
Prime_Gen.entry.selection_range(0, Tkinter.END)
def OnGenButtonClick(Prime_Gen):
n= str(Prime_Generation(Prime_Gen.entryVariable5.get(),Prime_Gen.entryVariable6.get()))
Prime_Gen.Result_label.insert(END,"\nPrimes Found\n")
Prime_Gen.Result_label.insert(END,n)
Prime_Gen.entry.focus_set()
Prime_Gen.entry.selection_range(0, Tkinter.END)
def Prime_Check_Win(self):
Prime_Check = Toplevel()
Prime_Check.grid()
Prime_Check.labelVariable8 = StringVar()
Title_label3 = Label(Prime_Check,textvariable=Prime_Check.labelVariable8,
relief = RAISED,fg="black",bg="white"
,font = "Arial")
Title_label3.grid(column=0,row=0,columnspan=4)
Prime_Check.labelVariable8.set(u"Please enter a Number to be checked for primality")
Prime_Check.labelVariable9 = StringVar()
SubTitle_label3 = Label(Prime_Check,textvariable=Prime_Check.labelVariable9,fg="black",bg="white")
SubTitle_label3.grid(column=0,row=1,columnspan=4)
Prime_Check.labelVariable9.set(u"(Please enter values no greater than 10 million)")
Prime_Check.entryVariable = StringVar()
Prime_Check.entry = Entry(Prime_Check,textvariable=Prime_Check.entryVariable)
Prime_Check.entry.grid(column=0,row=2)
Prime_Check.entryVariable.set(u"Enter Number here.")
Checkbutton = Button(Prime_Check,text=u"Check !",command=self.OnCheckButtonClick
,bg="blue",fg="white",relief=RAISED,padx=10,pady=10
,activebackground="red",activeforeground="white")
Checkbutton.grid(column=0,row=4)
Prime_Check.labelVariable10 = StringVar()
Result_label2 = Message(Prime_Check,textvariable=Prime_Check.labelVariable10
,anchor="w",fg="blue",bg="white"
,width=500,relief = SUNKEN,padx=3,pady=3)
Result_label2.grid(column=0,row=5,columnspan=2,rowspan=100)
Prime_Check.labelVariable10.set(u"Hello")
Prime_Check.grid_columnconfigure(0,weight=1)
Prime_Check.resizable(True,False)
Prime_Check.update()
Prime_Check.geometry(Prime_Check.geometry())
Prime_Check.entry.focus_set()
Prime_Check.entry.selection_range(0, Tkinter.END)
def OnCheckButtonClick(Prime_Check):
Prime_Check.labelVariable10.set(Prime_Checker(Prime_Check.entryVariable.get())) #Had to call on prime gen and display results
Prime_Check.entry.focus_set()
Prime_Check.entry.selection_range(0, Tkinter.END)
def Nth_Prime_Win(self):
Nth_Prime = Toplevel()
Nth_Prime.grid()
Nth_Prime.labelVariable12 = StringVar()
Title_label = Label(Nth_Prime,textvariable=Nth_Prime.labelVariable12,
relief = RAISED,fg="black",bg="white"
,font = "Arial")
Title_label.grid(column=0,row=0,columnspan=4)
Nth_Prime.labelVariable12.set(u"Please enter the Nth prime you would like to find")
Nth_Prime.labelVariable13 = StringVar()
SubTitle_label = Label(Nth_Prime,textvariable=Nth_Prime.labelVariable13,fg="black",bg="white")
SubTitle_label.grid(column=0,row=1,columnspan=4)
Nth_Prime.labelVariable13.set(u"(Please enter values no greater than 664579")
Nth_Prime.entryVariable = StringVar()
Nth_Prime.entry = Entry(Nth_Prime,textvariable=Nth_Prime.entryVariable)
Nth_Prime.entry.grid(column=0,row=4)
Nth_Prime.entryVariable.set(u"Enter Number here.")
Genbutton = Button(Nth_Prime,text=u"Generate !",command=self.OnButtonNthClick
,bg="green",relief=RAISED,padx=10,pady=10
,activebackground="red",activeforeground="white")
Genbutton.grid(column=0,row=5)
Nth_Prime.labelVariable14 = StringVar()
Result_label = Message(Nth_Prime,textvariable=Nth_Prime.labelVariable14
,anchor="w",fg="blue",bg="white"
,width=1000,relief = SUNKEN,justify=LEFT,padx=3,pady=3)
Result_label.grid(column=0,row=6,columnspan=2,rowspan=100)
Nth_Prime.labelVariable14.set(u"Hello")
Nth_Prime.grid_columnconfigure(0,weight=1)
Nth_Prime.resizable(False,False)
Nth_Prime.update()
Nth_Prime.geometry(Nth_Prime.geometry())
Nth_Prime.entry.focus_set()
Nth_Prime.entry.selection_range(0, Tkinter.END)
def OnButtonNthClick(Nth_Prime):
Nth_Prime.labelVariable14.set(NthPrime(Nth_Prime.entryVariable.get()))
Nth_Prime.entry.focus_set()
Nth_Prime.entry.selection_range(0, Tkinter.END)
def initialize(self):
self.grid()
self.labelVariable1 = StringVar()
Title_label1 = Label(self,textvariable=self.labelVariable1,
relief = RAISED,fg="black",bg="white"
,font = "Arial")
Title_label1.grid(column=0,row=0,columnspan=4)
self.labelVariable1.set(u"Welcome to the Prime Program")
self.labelVariable2 = StringVar()
SubTitle_label = Label(self,textvariable=self.labelVariable2,fg="black",bg="white")
SubTitle_label.grid(column=0,row=1,columnspan=4)
self.labelVariable2.set(u"(Please select the function you would like to use)")
PrimeGenbutton = Button(self,text=u"Find Primes between 2 limits !",command=self.Prime_Gen_Win
,bg="yellow",relief=RAISED,padx=10,pady=10
,activebackground="red",activeforeground="white")
PrimeGenbutton.grid(column=0,row=3)
PrimeCheckbutton = Button(self,text=u"Check if a number is prime !",command=self.Prime_Check_Win
,bg="blue",fg="white",relief=RAISED,padx=14,pady=10
,activebackground="red",activeforeground="white")
PrimeCheckbutton.grid(column=0,row=4)
NthPrimebutton = Button(self,text=u"Find the Nth prime !",command=self.Nth_Prime_Win
,bg="green",relief=RAISED,padx=35,pady=10
,activebackground="red",activeforeground="white")
NthPrimebutton.grid(column=0,row=5)
self.grid_columnconfigure(0,weight=1)
self.resizable(False,False)
self.update()
self.geometry(self.geometry())
if __name__ == "__main__":
app = Prime_app_tk(None)
app.title('Prime Program')
app.mainloop()
There's no quick fix for your code. It attempts to be object-oriented, but is doing so incorrectly. You need to properly define your methods, and should also adhere to PEP8 naming conventions -- specifically, methods and functions should start with a lowercase, and classes should start with an uppercase. Because you don't follow PEP8, and because of the odd way you use Prime_Gen to mean different things at different times, your code is incredibly hard to understand.
The crux of the problem is that inside OnGenButtonClick, Prime_Gen is not what you think it is. It is an instance of Prime_app_tk rather than the value that you set the local variable Prime_Gen to in Prime_Gen_Win. Thus, any attributes you assigned to the original Prime_Gen don't exist in this other Prime_Gen.
The reason is that the button is defined like this:
Genbutton = Button(..., command=self.OnGenButtonClick, ...)
In this context, self is the instance of Prime_app_tk, so that becomes the parameter passed to OnGenButtonClick. Inside that function you call the parameter Prime_Gen, in spite of the universal convention to name it self. This causes confusion in your code, because you also have a local variable named Prime_Gen in the code that creates the toplevel window.