Advertisement
MKbear

Converter and Graph

Sep 29th, 2022
681
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.76 KB | None | 0 0
  1. import dearpygui as dearpy
  2. import dearpygui.dearpygui as dpg, xml.dom.minidom, urllib.request
  3. from datetime import datetime
  4.  
  5.  
  6. def App():
  7.     Process()
  8.     CreateMainMenu()
  9.  
  10.  
  11. def CreateMainMenu():
  12.     dpg.create_context()
  13.     with dpg.font_registry():
  14.         with dpg.font("Resources/SegoeUI.ttf", 20, default_font=True, id="DefFont"):
  15.             dpg.add_font_range_hint(dpg.mvFontRangeHint_Cyrillic)
  16.             dpg.bind_font("DefFont")
  17.     width, height, channels, data = dpg.load_image("Resources/Arrow-down.png")
  18.     with dpg.texture_registry():
  19.         dpg.add_static_texture(width=width, height=height, default_value=data, tag="arrow")
  20.  
  21.     with dpg.window(tag="Primary Window"):
  22.         with dpg.menu_bar():
  23.             dpg.add_menu_item(label="Cockverter", callback=CreateConverterWindow())
  24.             dpg.add_menu_item(label="Graph", callback=CreateGraph())
  25.  
  26.     dpg.create_viewport(title='Custom Title', width=800, height=600)
  27.     dpg.setup_dearpygui()
  28.     dpg.show_viewport()
  29.     dpg.set_primary_window("Primary Window", True)
  30.     dpg.start_dearpygui()
  31.     dpg.destroy_context()
  32.  
  33.  
  34. def CreateConverterWindow():
  35.     with dpg.window(label="Converter", width=320, height=320, tag="Conv"):
  36.         dpg.add_combo(items=keylog, tag="FirstVal")
  37.         dpg.add_input_float(tag="FloatVal")
  38.         dpg.add_image("arrow")
  39.         dpg.add_combo(items=keylog, tag="SecVal")
  40.         dpg.add_text(tag="ConvRes")
  41.         dpg.add_button(label="Convert", callback=Convert)
  42.  
  43.  
  44. def CreateGraph():
  45.     with dpg.window(label="Graph", width=320, height=320, tag="Graph"):
  46.         x = datetime.now()
  47.         nowyear = x.year
  48.         dpg.add_combo(items=keylog, tag="MyFirstVal")
  49.         dpg.add_spacer(height=10)
  50.         with dpg.group(horizontal=True):
  51.             dpg.add_combo(items=[i for i in range(1, 31)], width=75, tag="MyDay")
  52.             dpg.add_combo(items=[i for i in range(1, 13)], width=75, tag="MyMonth")
  53.             dpg.add_combo(items=[i for i in range(1990, nowyear + 1)], width=75, tag="MyYear")
  54.         dpg.add_spacer(height=25)
  55.         with dpg.group(horizontal=True):
  56.             dpg.add_combo(items=[i for i in range(1, 31)], width=75, tag="MyDay2")
  57.             dpg.add_combo(items=[i for i in range(1, 13)], width=75, tag="MyMonth2")
  58.             dpg.add_combo(items=[i for i in range(1990, nowyear + 1)], width=75, tag="MyYear2")
  59.         dpg.add_button(label="Рисуй!", callback=CreateGraphWindow)
  60.  
  61.  
  62. def ProcessForGraph():
  63.     MyValue = dpg.get_value("MyFirstVal")
  64.     id = GetCurrencyIdList(MyValue)
  65.     x = dpg.get_value("MyDay")
  66.     y = dpg.get_value("MyDay2")
  67.     xx = dpg.get_value("MyMonth")
  68.     yy = dpg.get_value("MyMonth2")
  69.     if x == '1' or x == '2' or x == '3' or x == '4' or x == '5' or x == '6' or x == '7' or x == '8' or x == '9':
  70.         x = '0' + x
  71.     if xx == '1' or xx == '2' or xx == '3' or xx == '4' or xx == '5' or xx == '6' or xx == '7' or xx == '8' or xx == '9':
  72.         xx = '0' + xx
  73.     if y == '1' or y == '2' or y == '3' or y == '4' or y == '5' or y == '6' or y == '7' or y == '8' or y == '9':
  74.         y = '0' + y
  75.     if yy == '1' or yy == '2' or yy == '3' or yy == '4' or yy == '5' or yy == '6' or yy == '7' or yy == '8' or yy == '9':
  76.         yy = '0' + yy
  77.     url_1 = 'https://cbr.ru/scripts/XML_dynamic.asp?date_req1=' + str(x) + '/' + str(xx) + '/' + str(dpg.get_value(
  78.         "MyYear")) + '&date_req2=' + str(y) + '/' + str(yy) + '/' + str(dpg.get_value("MyYear2")) + '&VAL_NM_RQ=' + str(
  79.         id)
  80.     res = urllib.request.urlopen(str(url_1))
  81.     dom = xml.dom.minidom.parse(res)
  82.     dom.normalize()
  83.     nodeArray = dom.getElementsByTagName("Record")
  84.     curs_list = []
  85.     for node in nodeArray:
  86.         childList = node.childNodes
  87.         for child in childList:
  88.             if child.nodeName == 'Value':
  89.                 curs_list.append(float(child.childNodes[0].nodeValue.replace(',', '.')))
  90.     return curs_list
  91.  
  92.  
  93. def ProcessX():
  94.     MyValue = dpg.get_value("MyFirstVal")
  95.     id = GetCurrencyIdList(MyValue)
  96.     x = dpg.get_value("MyDay")
  97.     y = dpg.get_value("MyDay2")
  98.     xx = dpg.get_value("MyMonth")
  99.     yy = dpg.get_value("MyMonth2")
  100.     if x == '1' or x == '2' or x == '3' or x == '4' or x == '5' or x == '6' or x == '7' or x == '8' or x == '9':
  101.         x = '0' + x
  102.     if xx == '1' or xx == '2' or xx == '3' or xx == '4' or xx == '5' or xx == '6' or xx == '7' or xx == '8' or xx == '9':
  103.         xx = '0' + xx
  104.     if y == '1' or y == '2' or y == '3' or y == '4' or y == '5' or y == '6' or y == '7' or y == '8' or y == '9':
  105.         y = '0' + y
  106.     if yy == '1' or yy == '2' or yy == '3' or yy == '4' or yy == '5' or yy == '6' or yy == '7' or yy == '8' or yy == '9':
  107.         yy = '0' + yy
  108.     url_1 = 'https://cbr.ru/scripts/XML_dynamic.asp?date_req1=' + str(x) + '/' + str(xx) + '/' + str(dpg.get_value(
  109.         "MyYear")) + '&date_req2=' + str(y) + '/' + str(yy) + '/' + str(dpg.get_value("MyYear2")) + '&VAL_NM_RQ=' + str(id)
  110.     print(url_1)
  111.     res = urllib.request.urlopen(str(url_1))
  112.     dom = xml.dom.minidom.parse(res)
  113.     dom.normalize()
  114.     nodeArray = dom.getElementsByTagName("Record")
  115.     curs_list = []
  116.     for node in nodeArray:
  117.         curs_list.append(node.getAttribute('Date'))
  118.     return print(curs_list)
  119.  
  120.  
  121. def CreateGraphWindow():
  122.     sindatay = ProcessForGraph()
  123.     sindatax = [i for i in range(1, len(sindatay))]
  124.     with dpg.window(label="Tutorial"):
  125.         # create plot
  126.         with dpg.plot(label="Line Series", height=400, width=400):
  127.             # REQUIRED: create x and y axes
  128.             dpg.add_plot_axis(dpg.mvXAxis, label="x")
  129.             dpg.add_plot_axis(dpg.mvYAxis, label="y", tag="y_axis")
  130.             # series belong to a y axis
  131.             dpg.add_line_series(sindatax, sindatay, parent="y_axis")
  132.  
  133.  
  134. def Process():
  135.     global keylog, currency
  136.     now = datetime.now()
  137.     if now.day < 10:
  138.         u = 'http://www.cbr.ru/scripts/XML_daily.asp?date_req=' + "0" + str(now.day) + '/' + '0' + str(
  139.             now.month) + '/' + str(
  140.             now.year)
  141.     else:
  142.         u = 'http://www.cbr.ru/scripts/XML_daily.asp?date_req=' + str(now.day) + '/' + '0' + str(
  143.             now.month) + '/' + str(now.year)
  144.     res = urllib.request.urlopen(str(u))
  145.     dom = xml.dom.minidom.parse(res)
  146.     dom.normalize()
  147.     nodeArray = dom.getElementsByTagName("Valute")
  148.     currency = {}
  149.     keylog = []
  150.     for node in nodeArray:
  151.         childList = node.childNodes
  152.         for child in childList:
  153.             if child.nodeName == 'Name':
  154.                 currency.update({child.childNodes[0].nodeValue: 0})
  155.             if child.nodeName == 'Value':
  156.                 value = child.childNodes[0].nodeValue.replace(',', '.')
  157.                 currency.update({list(currency)[-1]: value})
  158.     for key in currency.keys():
  159.         keylog.append(key)
  160.  
  161.     print(keylog, currency)
  162.  
  163.  
  164. def GetCurrencyIdList(cur):
  165.     u = 'http://www.cbr.ru/scripts/XML_val.asp?d=0'
  166.     res = urllib.request.urlopen(str(u))
  167.     dom = xml.dom.minidom.parse(res)
  168.     dom.normalize()
  169.     nodeArray = dom.getElementsByTagName("Item")
  170.     currency_id = {}
  171.     for node in nodeArray:
  172.         childList = node.childNodes
  173.         for child in childList:
  174.             if child.nodeName == 'Name':
  175.                 currency_id.update({child.childNodes[0].nodeValue: 0})
  176.             if child.nodeName == 'ParentCode':
  177.                 value = child.childNodes[0].nodeValue.replace('    ', '')
  178.                 currency_id.update({list(currency_id)[-1]: value})
  179.     return currency_id.get(cur)
  180.  
  181.  
  182. def Convert():
  183.     First = currency[dpg.get_value("FirstVal")]
  184.     FloatValue = dpg.get_value("FloatVal")
  185.     Second = currency[dpg.get_value("SecVal")]
  186.     print(First, FloatValue, Second)
  187.  
  188.     Res = (float(First) / float(Second)) * FloatValue
  189.     dpg.set_value("ConvRes", Res)
  190.  
  191.  
  192. if __name__ == "__main__":
  193.     App()
  194.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement