HELP: Tkinter Text Widget problem

Reply

Join Date: Feb 2008
Posts: 2
Reputation: katak_guy is an unknown quantity at this point 
Solved Threads: 0
katak_guy katak_guy is offline Offline
Newbie Poster

HELP: Tkinter Text Widget problem

 
0
  #1
Feb 26th, 2008
How do i access the '\n' (newline) character in text widget? because I've a problem doing my project.


I would like to ignore the character but .........

if (A2int[plain[i]]=='\n') do the trick. Any ideas?

KeyError: '\n'
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,536
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 170
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: HELP: Tkinter Text Widget problem

 
0
  #2
Feb 26th, 2008
Honestly, few of us are mind-readers, you need to give us more information and code.
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 2
Reputation: katak_guy is an unknown quantity at this point 
Solved Threads: 0
katak_guy katak_guy is offline Offline
Newbie Poster

Re: HELP: Tkinter Text Widget problem

 
0
  #3
Feb 27th, 2008
bah, im sorry about the insufficient information to my question.

I am doing a program to encrypt a text from plaintext to ciphertext and the other way around, using Caesar Cipher idea. The only difference is this program has two keys. One to encrypt characters in odd position and the second key is to encrypt the characters in even position.

The key is in binary form due to the requirement of my system implementing binary input. It converts binary to decimal then uses the decimal value to add with the ascii value for the result = ciphertext.

When i was using text widget somehow '\n' exist and i've only mapped characters from A-Z and space. So I thought of doing it by mapping the '\n' character with a value.

Is there any better solution to my codings? Any improvement to be done and suggestions?
I've only started learning python 4 days ago. Sorry for the newb post =( .The code is posted as below.

  1. from Tkinter import *
  2.  
  3. class App(object):
  4.  
  5. def __init__(self, master):
  6. frame = Frame(master, bg="black", borderwidth=30)
  7. frame.pack(expand=NO)
  8.  
  9. self.enc=Button(frame, width=12, font = "Verdana 14 bold",
  10. text="Encrypt", command=self.caesar_encrypt)
  11. self.dec=Button(frame, width=12, font = "Verdana 14 bold",
  12. text="Decrypt", command=self.caesar_decrypt)
  13. self.exit=Button(frame, width=12, font = "Verdana 14 bold",
  14. text="Clear All", command=self.clear_all)
  15.  
  16. Label(frame, font = "Verdana 14 bold", fg='cyan', bg="black", text="Key").grid(row=0, column=1)
  17. Label(frame, font = "Verdana 14 bold", fg='cyan', bg="black", text="Plaintext").grid(row=2, column=1)
  18. Label(frame, font = "Verdana 14 bold", fg='cyan', bg="black", text="Ciphertext").grid(row=4, column=1)
  19.  
  20. self.entry1=Entry(frame, font = "Verdana 14", width=25, bg = 'orange')
  21. self.entry2=Entry(frame, font = "Verdana 14", width=25, bg = 'orange')
  22.  
  23. self.text3=Text(frame, font = "Verdana 14", width=50, height=10, bg = 'orange')
  24. self.text4=Text(frame, font = "Verdana 14", width=50, height=10, bg = 'orange')
  25.  
  26. self.entry5=Entry(frame, font = "Verdana 14", width=25, bg = 'orange')
  27. self.entry6=Entry(frame, font = "Verdana 14", width=25, bg = 'orange')
  28.  
  29. self.enc.grid(row=2, column=0)
  30. self.dec.grid(row=3, column=0)
  31. self.exit.grid(row=4, column=0)
  32.  
  33. self.entry1.grid(row=1, column=1, sticky=W)
  34. self.entry2.grid(row=1, column=1, sticky=E)
  35.  
  36. self.text3.grid(row=3, column=1)
  37. self.text4.grid(row=5, column=1)
  38.  
  39. self.entry5.grid(row=6, column=1, sticky=W)
  40. self.entry6.grid(row=6, column=1, sticky=E)
  41.  
  42. def clear_all(self):
  43. self.entry1.delete(0,END)
  44. self.entry2.delete(0,END)
  45.  
  46. self.text3.delete(1.0,END)
  47. self.text4.delete(1.0,END)
  48.  
  49. self.entry5.delete(0,END)
  50. self.entry6.delete(0,END)
  51.  
  52. def binary2dec(self):
  53. s = list(self.entry1.get().strip())
  54. s.reverse()
  55.  
  56. t = list(self.entry2.get().strip())
  57. t.reverse()
  58.  
  59. result1 = 0
  60. result2 = 0
  61.  
  62. for i, n in enumerate(s):
  63. result1 += int(s[i]) * pow(2, i)
  64. self.entry5.delete(0, END)
  65. self.entry5.insert(0, result1)
  66.  
  67. for i, n in enumerate(t):
  68. result2 += int(t[i]) * pow(2, i)
  69. self.entry6.delete(0, END)
  70. self.entry6.insert(0, result2)
  71.  
  72. return result1, result2
  73.  
  74.  
  75. def caesar_encrypt(self):
  76. A2int = {'A':0, 'B':1, 'C':2, 'D':3, 'E':4, 'F':5, 'G':6,'H':7,'I':8, 'J':9, 'K':10,
  77. 'L':11, 'M':12, 'N':13, 'O':14, 'P':15, 'Q':16, 'R':17, 'S':18, 'T':19, 'U':20,
  78. 'V':21, 'W':22, 'X':23, 'Y':24, 'Z':25, ' ':26, '\n':999}
  79. int2A = {0:'A', 1:'B', 2:'C', 3:'D', 4:'E', 5:'F', 6:'G', 7:'H', 8:'I', 9:'J', 10:'K',
  80. 11:'L', 12:'M', 13:'N', 14:'O', 15:'P', 16:'Q', 17:'R', 18:'S', 19:'T', 20:'U',
  81. 21:'V', 22:'W', 23:'X', 24:'Y', 25:'Z', 26:' ', 999:'\n'}
  82. plain = self.text3.get(1.0,END).upper()
  83. key_odd, key_even = self.binary2dec()
  84. ciphertext = ''
  85.  
  86. for i, n in enumerate(plain):
  87. a='';
  88. a=A2int[plain[i]]
  89. if (a==999): continue
  90. #ODD KEY
  91. if (i%2==1):
  92. n = A2int[plain[i]] + key_odd
  93. if (n == 26): n = n
  94. if (n > 26): n = n % 27
  95. ciphertext += int2A[n]
  96.  
  97. #EVEN KEY
  98. if (i%2==0):
  99.  
  100. n = A2int[plain[i]] + key_even
  101. if (n == 26): n = n
  102. if (n > 26): n = n % 27
  103. ciphertext += int2A[n]
  104.  
  105.  
  106. self.text4.delete(1.0, END)
  107. self.text4.insert(1.0, ciphertext)
  108.  
  109. def caesar_decrypt(self):
  110. A2int = {'A':0, 'B':1, 'C':2, 'D':3, 'E':4, 'F':5, 'G':6,'H':7,'I':8, 'J':9, 'K':10,
  111. 'L':11, 'M':12, 'N':13, 'O':14, 'P':15, 'Q':16, 'R':17, 'S':18, 'T':19, 'U':20,
  112. 'V':21, 'W':22, 'X':23, 'Y':24, 'Z':25, ' ':26, '\n':999}
  113. int2A = {0:'A', 1:'B', 2:'C', 3:'D', 4:'E', 5:'F', 6:'G', 7:'H', 8:'I', 9:'J', 10:'K',
  114. 11:'L', 12:'M', 13:'N', 14:'O', 15:'P', 16:'Q', 17:'R', 18:'S', 19:'T', 20:'U',
  115. 21:'V', 22:'W', 23:'X', 24:'Y', 25:'Z', 26:' ', 999:'\n'}
  116. cipher = self.text4.get(1.0,END).upper()
  117. key_odd, key_even = self.binary2dec()
  118. plaintext = ''
  119.  
  120.  
  121. for i, n in enumerate(cipher):
  122. a='';
  123. a=A2int[cipher[i]]
  124. if (a==999): continue
  125. #ODD KEY
  126. if (i%2==1):
  127. n = A2int[cipher[i]] - key_odd
  128. while (n<0): n = n + 27
  129. plaintext += int2A[n]
  130. #EVEN KEY
  131. if (i%2==0):
  132. n = A2int[cipher[i]] - key_even
  133. while (n<0): n = n + 27
  134. plaintext += int2A[n]
  135.  
  136. self.text3.delete(1.0, END)
  137. self.text3.insert(1.0, plaintext)
  138.  
  139. root = Tk()
  140. root.title("Caesar Cipher")
  141. root.maxsize(834,695)
  142. app = App(root)
  143.  
  144. root.mainloop()
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 237
Reputation: katharnakh is an unknown quantity at this point 
Solved Threads: 33
katharnakh's Avatar
katharnakh katharnakh is offline Offline
Posting Whiz in Training

Re: HELP: Tkinter Text Widget problem

 
0
  #4
Feb 28th, 2008
Originally Posted by katak_guy View Post
How do i access the '\n' (newline) character in text widget? because I've a problem doing my project.


I would like to ignore the character but .........

if (A2int[plain[i]]=='\n') do the trick. Any ideas?

KeyError: '\n'
hey, if you are asking how to get index of '\n' character or to find whether string is having that character, then
  1. from Tkinter import *
  2.  
  3. root = Tk()
  4.  
  5. def func():
  6. if '\n' in text.get(1.0, END):
  7. print 'at: ', text.get(1.0, END).index('\n')
  8.  
  9. text = Text(root, height=10, width=50)
  10. text.grid(row=0, column=0)
  11. b = Button(root, text='click', command=func)
  12. b.grid(row=1)
  13.  
  14. root.mainloop()
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC